Searching for Items in Microsoft PropertyGrid

Applies to TestComplete 15.63, last modified on April 23, 2024

A common usage of the PropertyGrid control in applications is to provide a visual interface for configuring settings of the application and its objects. When testing an application that uses PropertyGrid controls, you may need to check whether the grid contains a specific item. This way, you can ensure that all of the needed settings are exposed for displaying and adjusting them in the PropertyGrid.

You can determine whether the PropertyGrid contains a specific item by iterating through the grid items in a loop. On each iteration, you can check whether the item label (the wLabel property value) matches the specified string.

To perform these actions, TestComplete should have access to internal objects, properties and methods of the PropertyGrid control. For this purpose, the .NET Application Support and Microsoft Control Support plugins must be installed and enabled.

When testing Microsoft PropertyGrid controls, use specific methods and properties of the corresponding MicrosoftPropertyGrid object. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with an object’s properties and methods from your scripts. However, when testing a PropertyGrid control from your keyword test, you can use the same methods and properties calling them from keyword test operations. For more information, see Keyword Tests Basic Operations.

The following example demonstrates how this approach can be implemented. The script contains the following routines:

  • Main is the “main” routine. It obtains the scripting object that corresponds to the PropertyGrid control and checks whether it contains the “Appearance.Font.Name” and “Misc.Version” items.
  • The Contains function searches for the specified item in the PropertyGrid control. If the item was found, the function returns True; otherwise False. The function has two parameters:
    • Grid - The tested PropertyGrid control.
    • FullLabel - Specifies the full label of the sought-for item (for example, “Appearance.Font.Name”).

JavaScript

function Main ()
{
  var p, Grid, HasItem;

  // Obtain the grid object
  p = Sys.Process("PropertyGridSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("propertyGrid1");

  // Check if the grid contains specific items
  if (Contains (Grid, "Appearance.Font.Name"))
    Log.Message ("PropertyGrid contains the \"Appearance.Font.Name\" item.")
  else
    Log.Message ("PropertyGrid does not contain the \"Appearance.Font.Name\" item.");

  if (Contains (Grid, "Misc.Version"))
    Log.Message ("PropertyGrid contains the \"Misc.Version\" item.")
  else
    Log.Message ("PropertyGrid does not contain the \"Misc.Version\" item.");
}

// Returns True if PropertyGrid contains the specified item
function Contains (Grid, FullLabel)
{
  var Labels, Items, itemFound;

  // Split the full label into parts
  Labels = FullLabel.split(".");

  Items = Grid;
  // Walk down the item hierarchy
  for (let i=0; i<Labels.length; i++)
  {
    itemFound = false;

    // Iterate through the items and compare their labels
    for (let j=0; j<Items.wItemCount; j++)
      if (equal(Items.wLabel(j), Labels[i]))
      {
        itemFound = true; // Item's label matches the specified one
        break;
      }

    if (itemFound)
      Items = Items.wItems(j) // Item was found; continue searching
    else
      return false; // Item was not found
  }

  return true;
}

JScript

function Main ()
{
  var p, Grid, HasItem;

  // Obtain the grid object
  p = Sys.Process("PropertyGridSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("propertyGrid1");

  // Check if the grid contains specific items
  if (Contains (Grid, "Appearance.Font.Name"))
    Log.Message ("PropertyGrid contains the \"Appearance.Font.Name\" item.")
  else
    Log.Message ("PropertyGrid does not contain the \"Appearance.Font.Name\" item.");

  if (Contains (Grid, "Misc.Version"))
    Log.Message ("PropertyGrid contains the \"Misc.Version\" item.")
  else
    Log.Message ("PropertyGrid does not contain the \"Misc.Version\" item.");
}

// Returns True if PropertyGrid contains the specified item
function Contains (Grid, FullLabel)
{
  var Labels, Items, itemFound, i, j;

  // Split the full label into parts
  Labels = FullLabel.split(".");

  Items = Grid;
  // Walk down the item hierarchy
  for (i=0; i<Labels.length; i++)
  {
    itemFound = false;

    // Iterate through the items and compare their labels
    for (j=0; j<Items.wItemCount; j++)
      if (Items.wLabel(j) == Labels[i])
      {
        itemFound = true; // Item's label matches the specified one
        break;
      }

    if (itemFound)
      Items = Items.wItems(j) // Item was found; continue searching
    else
      return false; // Item was not found
  }

  return true;
}

Python

def Main ():

  # Obtain the grid object
  p = Sys.Process("PropertyGridSample")
  Grid = p.WinFormsObject("Form1").WinFormsObject("propertyGrid1")

  # Check if the grid contains specific items
  if (Contains (Grid, "Appearance.Font.Name")):
    Log.Message ("PropertyGrid contains the \"Appearance.Font.Name\" item.")
  else:
    Log.Message ("PropertyGrid does not contain the \"Appearance.Font.Name\" item.")

  if (Contains (Grid, "Misc.Version")):
    Log.Message ("PropertyGrid contains the \"Misc.Version\" item.")
  else:
    Log.Message ("PropertyGrid does not contain the \"Misc.Version\" item.")

# Returns True if PropertyGrid contains the specified item
def Contains (Grid, FullLabel):

  # Split the full label into parts
  Labels = FullLabel.split(".")

  Items = Grid
  # Walk down the item hierarchy
  for i in range(0, Labels.length-1):
    itemFound = False

    # Iterate through the items and compare their labels
    for j in range(0, Items.wItemCount-1):
      if (Items.wLabel[j] == Labels[i]):
        itemFound = True # Item's label matches the specified one
        break

    if (itemFound):
      Items = Items.wItems[j] # Item was found continue searching
    else:
      return False # Item was not found

  return True

VBScript

Sub Main
  Dim p, Grid, HasItem

  ' Obtain the grid object
  Set p = Sys.Process ("PropertyGridSample")
  Set Grid = p.WinFormsObject("Form1").WinFormsObject("propertyGrid1")

  ' Check if the grid contains specific items
  If Contains (Grid, "Appearance.Font.Name") Then
    Call Log.Message ("PropertyGrid contains the ""Appearance.Font.Name"" item.")
  Else
    Call Log.Message ("PropertyGrid does not contain the ""Appearance.Font.Name"" item.")
  End If

  If Contains (Grid, "Misc.Version") Then
    Call Log.Message ("PropertyGrid contains the ""Misc.Version"" item.")
  Else
    Call Log.Message ("PropertyGrid does not contain the ""Misc.Version"" item.")
  End If
End Sub

' Returns True if PropertyGrid contains the specified item
Function Contains (Grid, FullLabel)
  Dim Labels, Items, itemFound, i, j

  ' Split the full label into parts
  Labels = Split(FullLabel, ".")

  ' Walk down the item hierarchy
  Set Items = Grid
  For i = 0 To UBound(Labels)
    itemFound = False

    ' Iterate through the items and compare their labels
    For j = 0 To Items.wItemCount-1
      If Items.wLabel(j) = Labels(i) Then 
        itemFound = True' Item's label matches the specified one
        Exit For
      End If
    Next

    If itemFound Then
      Set Items = Items.wItems(j) ' Item was found; continue searching
    Else
      Contains = False' Item was not found
      Exit Function
    End If
  Next

  Contains = True
End Function

DelphiScript

// Returns True if PropertyGrid contains the specified item
function Contains (Grid, FullLabel);
var nLabels, Items, itemFound, i, j : OleVariant;
begin
  aqString.ListSeparator := '.';
  nLabels := aqString.GetListLength (FullLabel);

  // Walk down the item hierarchy
  Items := Grid;
  for i:=0 to nLabels-1 do
  begin
    itemFound := false;

    // Iterate through the items and compare their labels
    for j:=0 to Items.wItemCount-1 do
      if Items.wLabel[j] = aqString.GetListItem(FullLabel, i) then
      begin
        itemFound := true; // Item's label matches the specified one
        Break;
      end;

    // Check if the item was found
    if itemFound then
      Items := Items.wItems[j] // Item was found; continue searching
    else
    begin
      Result := false; // Item was not found
      Exit
    end
  end;

  Result := true;
end;

procedure Main;
var p, Grid, HasItem : OleVariant;
begin
  // Obtain the grid object
  p := Sys.Process ('PropertyGridSample');
  Grid := p.WinFormsObject('Form1').WinFormsObject('propertyGrid1');

  // Check if the grid contains specific items
  if Contains (Grid, 'Appearance.Font.Name') then
    Log.Message ('PropertyGrid contains the "Appearance.Font.Name" item.')
  else
    Log.Message ('PropertyGrid does not contain the "Appearance.Font.Name" item.');

  if Contains (Grid, 'Misc.Version') then
    Log.Message ('PropertyGrid contains the "Misc.Version" item.')
  else
    Log.Message ('PropertyGrid does not contain the "Misc.Version" item.');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, HasItem;

  // Obtain the grid object
  p = Sys["Process"]("PropertyGridSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("propertyGrid1");

  // Check if the grid contains specific items
  if (Contains (Grid, "Appearance.Font.Name"))
    Log["Message"]("PropertyGrid contains the \"Appearance.Font.Name\" item.")
  else
    Log["Message"]("PropertyGrid does not contain the \"Appearance.Font.Name\" item.");

  if (Contains (Grid, "Misc.Version"))
    Log["Message"]("PropertyGrid contains the \"Misc.Version\" item.")
  else
    Log["Message"]("PropertyGrid does not contain the \"Misc.Version\" item.");
}

// Returns True if PropertyGrid contains the specified item
function Contains (Grid, FullLabel)
{
  var Labels, Items, itemFound, i, j;

  // Split the full label into parts
  Labels = FullLabel["split"](".");

  Items = Grid;
  // Walk down the item hierarchy
  for (i=0; i<Labels["length"]; i++)
  {
    itemFound = false;

    // Iterate through the items and compare their labels
    for (j=0; j<Items["wItemCount"]; j++)
      if (Items["wLabel"](j) == Labels[i])
      {
        itemFound = true; // Item's label matches the specified one
        break;
      }

    if (itemFound)
      Items = Items["wItems"](j) // Item was found; continue searching
    else
      return false; // Item was not found
  }

  return true;
}

See Also

Working With Microsoft PropertyGrid
Accessing Items in Microsoft PropertyGrid
Iterating Through Items in Microsoft PropertyGrid

Highlight search results