Expanding and Collapsing Items in Microsoft PropertyGrid

Applies to TestComplete 15.69, last modified on November 13, 2024

The PropertyGrid control displays its entries in a hierarchical form; items may be displayed under categories, they may have nested items, and so on. The user can expand the categories and items to view the nested items, or collapse them in order to simplify the grid view. This topic explains various approaches that can be used to expand and collapse PropertyGrid items.

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.

Using the MicrosoftPropertyGrid.Expand and Collapse Actions

The MicrosoftPropertyGrid object provides the Expand and Collapse actions that can be used to expand and collapse items in the PropertyGrid control. To determine the expanded state of a particular item, you can use the wExpanded property.

JavaScript, JScript

function Main ()
{
  var p, Grid;

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

  Grid.Expand("Appearance");
  Grid.wItems("Appearance").Expand("Font");
  Grid.Collapse("Accessibility");
}

Python

def Main ():

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

  Grid.Expand("Appearance")
  Grid.wItems("Appearance").Expand("Font")
  Grid.Collapse("Accessibility")

VBScript

Sub Main
  Dim p, Grid

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

  Grid.Expand("Appearance")
  Grid.wItems("Appearance").Expand("Font")
  Grid.Collapse("Accessibility")
End Sub

DelphiScript

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

  Grid.Expand('Appearance');
  Grid.wItems('Appearance').Expand('Font');
  Grid.Collapse('Accessibility');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

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

  Grid["Expand"]("Appearance");
  Grid["wItems"]("Appearance")["Expand"]("Font");
  Grid["Collapse"]("Accessibility");
}

Simulating Mouse and Keyboard Actions

It is possible to expand PropertyGrid items by double-clicking their labels. You can simulate these clicks using the DblClickLabel action of the MicrosoftPropertyGrid or PropertyGridItemGroup objects:

JavaScript, JScript

function Main ()
{
  var p, Grid;

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

  Grid.DblClickLabel("Appearance");
  Grid.wItems("Appearance").DblClickLabel("Font");
  Grid.DblClickLabel("Accessibility");
}

Python

def Main ():

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

  Grid.DblClickLabel("Appearance")
  Grid.wItems("Appearance").DblClickLabel("Font")
  Grid.DblClickLabel("Accessibility")

VBScript

Sub Main
  Dim p, Grid

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

  Grid.DblClickLabel("Appearance")
  Grid.wItems("Appearance").DblClickLabel("Font")
  Grid.DblClickLabel("Accessibility")
End Sub

DelphiScript

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

  Grid.DblClickLabel('Appearance');
  Grid.wItems('Appearance').DblClickLabel('Font');
  Grid.DblClickLabel('Accessibility');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

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

  Grid["DblClickLabel"]("Appearance");
  Grid["wItems"]("Appearance")["DblClickLabel"]("Font");
  Grid["DblClickLabel"]("Accessibility");
}

Note: For items that do not have child items, a double-click on a label has another effect - it toggles the item value. So, use this approach only if you are sure that the item is expandable, otherwise it may affect data in the grid control.

You can also expand and collapse PropertyGrid items using special keyboard shortcuts. For example, Ctrl+Plus or Right Arrow expands the currently focused item, and Ctrl+Minus or Left Arrow collapses the selected item. To send these keystrokes to the grid control, use the Keys action. The following example demonstrates how to do this:

JavaScript, JScript

function Main ()
{
  var p, Grid;

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

  // Expand and collapse some items
  Grid.ClickLabel("Appearance");
  Grid.Keys("^[NumPlus]");
  Grid.wItems("Appearance").ClickLabel("Font");
  Grid.Keys("^[NumPlus]");
  Grid.ClickLabel("Accessibility");
  Grid.Keys("^[NumMinus]");
}

Python

def Main ():

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

  # Expand and collapse some items
  Grid.ClickLabel("Appearance")
  Grid.Keys("^[NumPlus]")
  Grid.wItems("Appearance").ClickLabel("Font")
  Grid.Keys("^[NumPlus]")
  Grid.ClickLabel("Accessibility")
  Grid.Keys("^[NumMinus]")

VBScript

Sub Main
  Dim p, Grid

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

  ' Expand and collapse some items
  Grid.ClickLabel("Appearance")
  Grid.Keys("^[NumPlus]")
  Grid.wItems("Appearance").ClickLabel("Font")
  Grid.Keys("^[NumPlus]")
  Grid.ClickLabel("Accessibility")
  Grid.Keys("^[NumMinus]")
End Sub

DelphiScript

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

  // Expand and collapse some items
  Grid.ClickLabel('Appearance');
  Grid.Keys('^[NumPlus]');
  Grid.wItems('Appearance').ClickLabel('Font');
  Grid.Keys('^[NumPlus]');
  Grid.ClickLabel('Accessibility');
  Grid.Keys('^[NumMinus]');
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

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

  // Expand and collapse some items
  Grid["ClickLabel"]("Appearance");
  Grid["Keys"]("^[NumPlus]");
  Grid["wItems"]("Appearance")["ClickLabel"]("Font");
  Grid["Keys"]("^[NumPlus]");
  Grid["ClickLabel"]("Accessibility");
  Grid["Keys"]("^[NumMinus]");
}

Using PropertyGrid’s Internal Members

The PropertyGrid control and its internal objects include special properties and methods that let you toggle the item’s expanded state. Note however, that using these native properties and methods does not involve any user interaction, so, this approach may not suit your needs.

To check or change the expanded state of a particular grid item, you can use its Expanded property. The example below demonstrates how you can use this property in scripts:

Example

View description

JavaScript

function Main ()
{
  var p, Grid;

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

  // Expand and collapse some items
  Expand (Grid, "Appearance");
  Expand (Grid, "Appearance.Font");
  Collapse (Grid, "Accessibility");
}

// Expands the item specified by its full label
function Expand (Grid, FullLabel)
{
  var Item = GetItem(Grid, FullLabel);
  Item.Expanded = true;
}

// Collapses the item specified by its full label
function Collapse (Grid, FullLabel)
{
  let Item = GetItem(Grid, FullLabel);
  Item.Expanded = false;
}

// Returns the item specified by its full label
function GetItem (Grid, FullLabel)
{
  var Labels, Coll, Item;

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

  // Get the top-level grid items
  Coll = Grid.GetPropEntries();

  // Walk down the hierarchy of items
  for (let i=0; i<Labels.length; i++)
  {
    // Obtain the item by its label
    Item = Coll.Item_2 (Labels[i]);
    if (strictEqual(Item, null))
      return null; // The item with the specified label is not found
    // Get child items of the current item
    Coll = Item.GridItems;
  }
  return Item;
}

JScript

function Main ()
{
  var p, Grid;

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

  // Expand and collapse some items
  Expand (Grid, "Appearance");
  Expand (Grid, "Appearance.Font");
  Collapse (Grid, "Accessibility");
}

// Expands the item specified by its full label
function Expand (Grid, FullLabel)
{
  var Item = GetItem(Grid, FullLabel);
  Item.Expanded = true;
}

// Collapses the item specified by its full label
function Collapse (Grid, FullLabel)
{
  var Item = GetItem(Grid, FullLabel);
  Item.Expanded = false;
}

// Returns the item specified by its full label
function GetItem (Grid, FullLabel)
{
  var Labels, Coll, Item, i;

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

  // Get the top-level grid items
  Coll = Grid.GetPropEntries();

  // Walk down the hierarchy of items
  for (i=0; i<Labels.length; i++)
  {
    // Obtain the item by its label
    Item = Coll.Item_2 (Labels[i]);
    if (Item == null)
      return null; // The item with the specified label is not found
    // Get child items of the current item
    Coll = Item.GridItems;
  }
  return Item;
}

Python

def Main ():

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

  # Expand and collapse some items
  Expand (Grid, "Appearance")
  Expand (Grid, "Appearance.Font")
  Collapse (Grid, "Accessibility")

# Expands the item specified by its full label
def Expand (Grid, FullLabel):
  Item = GetItem(Grid, FullLabel)
  Item.Expanded = True

# Collapses the item specified by its full label
def Collapse (Grid, FullLabel):
  Item = GetItem(Grid, FullLabel)
  Item.Expanded = False

# Returns the item specified by its full label
def GetItem (Grid, FullLabel):

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

  # Get the top-level grid items
  Coll = Grid.GetPropEntries()

  # Walk down the hierarchy of items
  for i in range(0, Labels.length-1):
    # Obtain the item by its label
    Item = Coll.Item_2 [Labels[i]]
    if (Item == None):
      return None # The item with the specified label is not found
    # Get child items of the current item
    Coll = Item.GridItems
  return Item

VBScript

Sub Main
  Dim p, Grid

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

  ' Expand and collapse some items
  Call Expand (Grid, "Appearance")
  Call Expand (Grid, "Appearance.Font")
  Call Collapse (Grid, "Accessibility")
End Sub

' Expands the item specified by its full label
Sub Expand (Grid, FullLabel)
  Dim Item
  Set Item = GetItem(Grid, FullLabel)
  Item.Expanded = True
End Sub

' Collapses the item specified by its full label
Sub Collapse (Grid, FullLabel)
  Dim Item
  Set Item = GetItem(Grid, FullLabel)
  Item.Expanded = False
End Sub

' Returns the item specified by its full label
Function GetItem (Grid, FullLabel)
  Dim Labels, Coll, Item, i

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

  ' Get the top-level grid items
  Set Coll = Grid.GetPropEntries

  ' Walk down the hierarchy of items
  For i=0 To UBound(Labels)
    ' Obtain the item by its label
    Set Item = Coll.Item_2 (Labels(i))
    If Item Is Nothing Then
      Set GetItem = Nothing ' The item with the specified label is not found
      Exit Function
    End If
    ' Get child items of the current item
    Set Coll = Item.GridItems
  Next
  Set GetItem = Item
End Function

DelphiScript

procedure Expand (Grid, FullLabel); forward;
procedure Collapse (Grid, FullLabel); forward;
function GetItem (Grid, FullLabel); forward;

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

  // Expand and collapse some items
  Expand(Grid, 'Appearance');
  Expand(Grid, 'Appearance.Font');
  Collapse(Grid, 'Accessibility');
end;

// Expands the item specified by its full label
procedure Expand (Grid, FullLabel);
var Item: OleVariant;
begin
  Item := GetItem(Grid, FullLabel);
  Item.Expanded := true;
end;

// Collapses the item specified by its full label
procedure Collapse (Grid, FullLabel);
var Item: OleVariant;
begin
  Item := GetItem(Grid, FullLabel);
  Item.Expanded := false;
end;

// Returns the item specified by its full label
function GetItem (Grid, FullLabel);
var nLabels, Coll, Item, i : OleVariant;
begin
  // Get the top-level grid items
  Coll := Grid.GetPropEntries;

  // Walk down the hierarchy of items
  aqString.ListSeparator := '.';
  nLabels := aqString.GetListLength (FullLabel);
  for i:=0 to nLabels-1 do
  begin
    // Obtain the item by its label
    Item := Coll.Item_2 (aqString.GetListItem (FullLabel, i));
    if Item = nil then
      Result := nil; // The item with the specified label is not found
    // Get child items of the current item
    Coll := Item.GridItems;
  end;
  Result := Item;
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

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

  // Expand and collapse some items
  Expand (Grid, "Appearance");
  Expand (Grid, "Appearance.Font");
  Collapse (Grid, "Accessibility");
}

// Expands the item specified by its full label
function Expand (Grid, FullLabel)
{
  var Item = GetItem(Grid, FullLabel);
  Item["Expanded"] = true;
}

// Collapses the item specified by its full label
function Collapse (Grid, FullLabel)
{
  var Item = GetItem(Grid, FullLabel);
  Item["Expanded"] = false;
}

// Returns the item specified by its full label
function GetItem (Grid, FullLabel)
{
  var Labels, Coll, Item, i;

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

  // Get the top-level grid items
  Coll = Grid["GetPropEntries"]();

  // Walk down the hierarchy of items
  for (i=0; i<Labels["length"]; i++)
  {
    // Obtain the item by its label
    Item = Coll["Item_2"](Labels[i]);
    if (Item == null)
      return null; // The item with the specified label is not found
    // Get child items of the current item
    Coll = Item["GridItems"];
  }
  return Item;
}

The PropertyGrid control also has the ExpandAllGridItems and CollapseAllGridItems methods that let you expand or collapse all grid items at once:

JavaScript, JScript

function Main ()
{
  var p, Grid;
  // Obtain the grid object
  p = Sys.Process ("PropertyGridSample");
  Grid = p.WinFormsObject("Form1").WinFormsObject("propertyGrid1");
  ...

  // Expand all items
  Grid.ExpandAllGridItems();
  ...

  // Collapse all items
  Grid.CollapseAllGridItems();
  ...
}

Python

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

  # Expand all items
  Grid.ExpandAllGridItems()
  ...

  # Collapse all items
  Grid.CollapseAllGridItems()
  ...

VBScript

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

  ' Expand all items
  Grid.ExpandAllGridItems
  ...

  ' Collapse all items
  Grid.CollapseAllGridItems
  ...
End Sub

DelphiScript

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

  // Expand all items
  Grid.ExpandAllGridItems;
  ...

  // Collapse all items
  Grid.CollapseAllGridItems;
  ...
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;
  // Obtain the grid object
  p = Sys["Process"]("PropertyGridSample");
  Grid = p["WinFormsObject"]("Form1")["WinFormsObject"]("propertyGrid1");
  ...

  // Expand all items
  Grid["ExpandAllGridItems"]();
  ...

  // Collapse all items
  Grid["CollapseAllGridItems"]();
  ...
}

See Also

Working With Microsoft PropertyGrid
Accessing Items in Microsoft PropertyGrid
Collapse Action (Specific to PropertyGrid Controls)
Expand Action (Specific to PropertyGrid Controls)
wExpanded Property (Specific to PropertyGrid Controls)

Highlight search results