Iterating Through Items in Microsoft PropertyGrid

Applies to TestComplete 14.60, last modified on April 22, 2021

Iterating through grid items means accessing items is a series, one by one. You may need to iterate through PropertyGrid items, for example, in order to perform the same set of operations on each item.

The MicrosoftPropertyGrid object, which is used to work with the PropertyGrid control in TestComplete, provides direct access to the top-level grid items. You can determine the number of top-level items using the wItemCount property. To iterate through the items, you need to organize a loop whose counter changes from 0 (index of the first item) to the wItemCount-1 (index of the last item). On each loop iteration, you can perform the needed actions with the current item. For example, you can retrieve its data and save it to a file.

Since the PropertyGrid items are organized in a hierarchy, you need to process items recursively. That is, after processing a single item, you need to apply the same processing to its child items. To access child items of a specific item, use the wItems property. It returns the PropertyGridItemGroup object that provides a scripting interface to the child items collection. You can iterate through the child items in the same way as iterating through top-level items; by using the wItemCount property.

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 example below iterates through the grid items in a loop and saves their data to an XML file.

Example

View description

JavaScript

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

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

  // Save grid data to an XML file
  FileName = "C:\\PropertyGridData.xml";
  ExportToXml (Grid, FileName);
  Log.File (FileName, "Exported grid data.");
}

function ExportToXml (Grid, FileName)
{
  var FSO, XmlFile, Overwrite, Unicode;

  // Create file and open it for writing
  Overwrite = true;
  Unicode = true;
  FSO = getActiveXObject("Scripting.FileSystemObject");
  XmlFile = FSO.CreateTextFile(FileName, Overwrite, Unicode);

  // Write the root tag
  XmlFile.WriteLine("<?xml version=\"1.0\"?>");
  XmlFile.WriteLine ("<grid>");

  // Save the grid data to the file
  ExportItemGroup(Grid, XmlFile, " ");
  
  // Close the root tag
  XmlFile.WriteLine("</grid>");

  // Close the file
  XmlFile.Close();
}

// Exports data of the specified item group
function ExportItemGroup (ItemGroup, XmlFile, indent)
{
  var ItemLabel, ItemValue, ItemText;

  for (let i=0; i<ItemGroup.wItemCount; i++)
  {
    // Get the item's label and value
    ItemLabel = ItemGroup.wLabel(i);
    ItemValue = ItemGroup.wValue(i);
    if (!strictEqual(ItemValue, null))
      ItemText = ReplaceSpecialChars(ItemValue.ToString().OleValue)
    else
      ItemText = "";

    // Write the item's label and value as attributes of the <item> tag
    XmlFile.Write (indent + "<item label=\"" + ItemLabel + "\" value=\"" + ItemText + "\"");

    // If the item has child items, ...
    if (ItemGroup.wItems(i).wItemCount > 0)
    {
       // ... then export them
       XmlFile.WriteLine (">"); // Close the opening <item> tag
       ExportItemGroup (ItemGroup.wItems(i), XmlFile, indent + " ");
       XmlFile.WriteLine (indent + "</item>"); // Write the closing tag
    }
    else
      XmlFile.WriteLine ("/>"); // Close the opening <item> tag
  }
}

// Replaces special XML characters in the specified string
function ReplaceSpecialChars (str)
{
  let s;
  s = aqString.Replace (str, "&", "&amp;", true);
  s = aqString.Replace (s, "<", "&lt;", true);
  s = aqString.Replace (s, ">", "&gt;", true);
  s = aqString.Replace (s, "\"", "&quot;", true);
  s = aqString.Replace (s, "'", "&apos;", true);
  return s;
}

JScript

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

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

  // Save grid data to an XML file
  FileName = "C:\\PropertyGridData.xml";
  ExportToXml (Grid, FileName);
  Log.File (FileName, "Exported grid data.");
}

function ExportToXml (Grid, FileName)
{
  var FSO, XmlFile, Overwrite, Unicode;

  // Create file and open it for writing
  Overwrite = true;
  Unicode = true;
  FSO = new ActiveXObject ("Scripting.FileSystemObject");
  XmlFile = FSO.CreateTextFile(FileName, Overwrite, Unicode);

  // Write the root tag
  XmlFile.WriteLine("<?xml version=\"1.0\"?>");
  XmlFile.WriteLine ("<grid>");

  // Save the grid data to the file
  ExportItemGroup(Grid, XmlFile, " ");
  
  // Close the root tag
  XmlFile.WriteLine("</grid>");

  // Close the file
  XmlFile.Close();
}

// Exports data of the specified item group
function ExportItemGroup (ItemGroup, XmlFile, indent)
{
  var i, ItemLabel, ItemValue, ItemText;

  for (i=0; i<ItemGroup.wItemCount; i++)
  {
    // Get the item's label and value
    ItemLabel = ItemGroup.wLabel(i);
    ItemValue = ItemGroup.wValue(i);
    if (ItemValue != null)
      ItemText = ReplaceSpecialChars(ItemValue.ToString().OleValue)
    else
      ItemText = "";

    // Write the item's label and value as attributes of the <item> tag
    XmlFile.Write (indent + "<item label=\"" + ItemLabel + "\" value=\"" + ItemText + "\"");

    // If the item has child items, ...
    if (ItemGroup.wItems(i).wItemCount > 0)
    {
       // ... then export them
       XmlFile.WriteLine (">"); // Close the opening <item> tag
       ExportItemGroup (ItemGroup.wItems(i), XmlFile, indent + " ");
       XmlFile.WriteLine (indent + "</item>"); // Write the closing tag
    }
    else
      XmlFile.WriteLine ("/>"); // Close the opening <item> tag
  }
}

// Replaces special XML characters in the specified string
function ReplaceSpecialChars (str)
{
  var s;
  s = aqString.Replace (str, "&", "&amp;", true);
  s = aqString.Replace (s, "<", "&lt;", true);
  s = aqString.Replace (s, ">", "&gt;", true);
  s = aqString.Replace (s, "\"", "&quot;", true);
  s = aqString.Replace (s, "'", "&apos;", true);
  return s;
}

Python

def Main ():

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

  # Save grid data to an XML file
  FileName = "C:\\PropertyGridData.xml"
  ExportToXml (Grid, FileName)
  Log.File (FileName, "Exported grid data.")

def ExportToXml (Grid, FileName):

  # Create file and open it for writing
  Overwrite = True
  Unicode = True
  FSO = Sys.OleObject['Scripting.FileSystemObject']
  XmlFile = FSO.CreateTextFile(FileName, Overwrite, Unicode)

  # Write the root tag
  XmlFile.WriteLine("<?xml version=\"1.0\"?>")
  XmlFile.WriteLine ("<grid>")

  # Save the grid data to the file
  ExportItemGroup(Grid, XmlFile, " ")
  
  # Close the root tag
  XmlFile.WriteLine("</grid>")

  # Close the file 
  XmlFile.Close()

# Exports data of the specified item group
def ExportItemGroup (ItemGroup, XmlFile, indent):

  for i in range(0, ItemGroup.wItemCount-1):
    # Get the item's label and value
    ItemLabel = ItemGroup.wLabel[i]
    ItemValue = ItemGroup.wValue[i]
    if (ItemValue != None):
      ItemText = ReplaceSpecialChars(ItemValue.ToString().OleValue)
    else:
      ItemText = ""

    # Write the item's label and value as attributes of the <item> tag
    XmlFile.Write (indent + "<item label=\"" + ItemLabel + "\" value=\"" + ItemText + "\"")

    # If the item has child items, ...
    if (ItemGroup.wItems[i].wItemCount > 0):
       # ... then export them
       XmlFile.WriteLine (">") # Close the opening <item> tag
       ExportItemGroup (ItemGroup.wItems(i), XmlFile, indent + " ")
       XmlFile.WriteLine (indent + "</item>") # Write the closing tag
    else:
      XmlFile.WriteLine ("/>") # Close the opening <item> tag

# Replaces special XML characters in the specified string
def ReplaceSpecialChars (str):
  s = aqString.Replace (str, "&", "&amp;", True)
  s = aqString.Replace (s, "<", "&lt;", True)
  s = aqString.Replace (s, ">", "&gt;", True)
  s = aqString.Replace (s, "\"", "&quot;", True)
  s = aqString.Replace (s, "'", "&apos;", True)
  return s

VBScript

Sub Main
  Dim p, Grid, FileName

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

  ' Save grid data to an XML file
  FileName = "C:\PropertyGridData.xml"
  Call ExportToXml (Grid, FileName)
  Call Log.File (FileName, "Exported grid data.")
End Sub

Sub ExportToXml (Grid, FileName)
  Dim FSO, XmlFile, Overwrite, Unicode

  ' Create file and open it for writing
  Overwrite = True
  Unicode = True
  Set FSO = CreateObject ("Scripting.FileSystemObject")
  Set XmlFile = FSO.CreateTextFile(FileName, Overwrite, Unicode)

  ' Write the root tag
  Call XmlFile.WriteLine("<?xml version=""1.0""?>")
  XmlFile.WriteLine ("<grid>")

  ' Save the grid data to the file
  Call ExportItemGroup(Grid, XmlFile, " ")
  
  ' Close the root tag
  XmlFile.WriteLine("</grid>")

  ' Close the file
  XmlFile.Close
End Sub

' Exports data of the specified item group
Sub ExportItemGroup (ItemGroup, XmlFile, indent)
  Dim i, ItemLabel, ItemValue, ItemText

  For i = 0 To ItemGroup.wItemCount-1
    ' Get the item's label and value
    ItemLabel = ItemGroup.wLabel(i)
    Set ItemValue = ItemGroup.wValue(i)
    If Not (ItemValue Is Nothing) Then 
      ItemText = ReplaceSpecialChars(ItemValue.ToString.OleValue)
    Else
      ItemText = ""
    End If

    ' Write the item's label and value as attributes of the <item> tag
    XmlFile.Write (indent & "<item label=""" & ItemLabel & """ value=""" & ItemText & """")

    ' If the item has child items, ...
    If ItemGroup.wItems(i).wItemCount > 0 Then
       ' ... then export them
       XmlFile.WriteLine (">") ' Close the opening <item> tag
       Call ExportItemGroup (ItemGroup.wItems(i), XmlFile, indent & " ")
       XmlFile.WriteLine (indent & "</item>") ' Write the closing tag
    Else
      XmlFile.WriteLine ("/>") ' Close the opening <item> tag
    End If
  Next
End Sub

' Replaces special XML characters in the specified string
Function ReplaceSpecialChars (str)
  Dim s
  s = aqString.Replace (str, "&", "&amp;", True)
  s = aqString.Replace (s, "<", "&lt;", True)
  s = aqString.Replace (s, ">", "&gt;", True)
  s = aqString.Replace (s, """", "&quot;", True)
  s = aqString.Replace (s, "'", "&apos;", True)
  ReplaceSpecialChars = s
End Function

DelphiScript

procedure ExportToXml (Grid, FileName); forward;
function ExportItemGroup (ItemGroup, XmlFile, indent); forward;
function ReplaceSpecialChars (str); forward;

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

  // Save grid data to an XML file
  FileName := 'C:\PropertyGridData.xml';
  ExportToXml (Grid, FileName);
  Log.File (FileName, 'Exported grid data.');
end;

procedure ExportToXml (Grid, FileName);
var FSO, XmlFile, Overwrite, Unicode : OleVariant;
begin
  // Create file and open it for writing
  Overwrite := true;
  Unicode := true;
  FSO := Sys.OleObject('Scripting.FileSystemObject');
  XmlFile := FSO.CreateTextFile(FileName, Overwrite, Unicode);

  // Write the root tag
  XmlFile.WriteLine('<?xml version="1.0"?>');
  XmlFile.WriteLine ('<grid>');

  // Save the grid data to the file
  ExportItemGroup(Grid, XmlFile, ' ');
  
  // Close the root tag
  XmlFile.WriteLine('</grid>');

  // Close the file
  XmlFile.Close;
end;

// Exports data of the specified item group
function ExportItemGroup (ItemGroup, XmlFile, indent);
var i, ItemLabel, ItemValue, ItemText : OleVariant;
begin
  for i:=0 to ItemGroup.wItemCount-1 do
  begin
    // Get the item's label and value
    ItemLabel := ItemGroup.wLabel[i];
    ItemValue := ItemGroup.wValue[i];
    if ItemValue <> nil then
      ItemText := ReplaceSpecialChars(ItemValue.ToString.OleValue)
    else
      ItemText := '';

    // Write the item's label and value as attributes of the <item> tag
    XmlFile.Write (indent + '<item label="' + ItemLabel + '" value="' + ItemText + '"');

    // If the item has child items, ...
    if ItemGroup.wItems[i].wItemCount > 0 then
    begin
       // ... then export them
       XmlFile.WriteLine ('>'); // Close the opening <item> tag
       ExportItemGroup (ItemGroup.wItems[i], XmlFile, indent + ' ');
       XmlFile.WriteLine (indent + '</item>'); // Write the closing tag
    end
    else
      XmlFile.WriteLine ('/>') // Close the opening <item> tag
  end
end;

// Replaces special XML characters in the specified string
function ReplaceSpecialChars (str);
var s : OleVariant;
begin
  s := aqString.Replace (str, '&', '&amp;', true);
  s := aqString.Replace (s, '<', '&lt;', true);
  s := aqString.Replace (s, '>', '&gt;', true);
  s := aqString.Replace (s, '"', '&quot;', true);
  s := aqString.Replace (s, '''', '&apos;', true);
  Result := s;
end;

C++Script, C#Script

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

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

  // Save grid data to an XML file
  FileName = "C:\\PropertyGridData.xml";
  ExportToXml (Grid, FileName);
  Log["File"](FileName, "Exported grid data.");
}

function ExportToXml (Grid, FileName)
{
  var FSO, XmlFile, Overwrite, Unicode;

  // Create file and open it for writing
  Overwrite = true;
  Unicode = true;
  FSO = new ActiveXObject ("Scripting.FileSystemObject");
  XmlFile = FSO["CreateTextFile"](FileName, Overwrite, Unicode);

  // Write the root tag
  XmlFile["WriteLine"]("<?xml version=\"1.0\"?>");
  XmlFile["WriteLine"]("<grid>");

  // Save the grid data to the file
  ExportItemGroup(Grid, XmlFile, " ");

  // Close the root tag
  XmlFile["WriteLine"]("</grid>");

  // Close the file
  XmlFile["Close"]();
}

// Exports data of the specified item group
function ExportItemGroup (ItemGroup, XmlFile, indent)
{
  var i, ItemLabel, ItemValue, ItemText;

  for (i=0; i<ItemGroup["wItemCount"]; i++)
  {
    // Get the item's label and value
    ItemLabel = ItemGroup["wLabel"](i);
    ItemValue = ItemGroup["wValue"](i);
    if (ItemValue != null)
      ItemText = ReplaceSpecialChars(ItemValue["ToString"]()["OleValue"])
    else
      ItemText = "";

    // Write the item's label and value as attributes of the <item> tag
    XmlFile["Write"](indent + "<item label=\"" + ItemLabel + "\" value=\"" + ItemText + "\"");

    // If the item has child items, ...
    if (ItemGroup["wItems"](i)["wItemCount"] > 0)
    {
       // ... then export them
       XmlFile["WriteLine"](">"); // Close the opening <item> tag
       ExportItemGroup (ItemGroup["wItems"](i), XmlFile, indent + " ");
       XmlFile["WriteLine"](indent + "</item>"); // Write the closing tag
    }
    else
      XmlFile["WriteLine"]("/>"); // Close the opening <item> tag
  }
}

// Replaces special XML characters in the specified string
function ReplaceSpecialChars (str)
{
  var s;
  s = aqString["Replace"](str, "&", "&amp;", true);
  s = aqString["Replace"](s, "<", "&lt;", true);
  s = aqString["Replace"](s, ">", "&gt;", true);
  s = aqString["Replace"](s, "\"", "&quot;", true);
  s = aqString["Replace"](s, "'", "&apos;", true);
  return s;
}

See Also

Working With Microsoft PropertyGrid
Obtaining and Setting Item Values in Microsoft PropertyGrid
Searching for Items in Microsoft PropertyGrid
wItems Property (Specific to PropertyGrid Controls)
wItemCount Property (Specific to PropertyGrid Controls)

Highlight search results