The PropertyGrid control displays its entries in a hierarchical form; properties may be displayed under categories, they may have nested properties, and so on. The users can expand the categories and rows to view their nested rows, or collapse them in order to simplify the grid view. In this post, I'll explain how you can expand and collapse PropertyGrid rows from scripts.
Simulating Keyboard Shortcuts
You can expand and collapse PropertyGrid rows using the corresponding keyboard shortcuts. For example, you can use the Right Arrow or Ctrl+Plus shortcut to expand the selected row, and the Left Arrow or Ctrl+Minus shortcut to collapse the selected row. To simulate these shortcuts, use the TestCompletes
Keys action applied to the grid control.
The example below illustrates how you can simulate the expand/collapse row keystrokes in the PropertyGrid. The example contains the following routines:
Main -- the main routine. It obtains the grid control and then expands and collapses the Appearance.Font row.
CollapseRow routines are used to expand and collapse the grid row. The Grid parameter specifies the tested PropertyGrid control, and FullLabel specifies the grid rows full label (e.g. Appearance.Font.Name). Both routines call the
SetRowExpanded routine to change the rows expanded state.
SetRowExpanded -- a helper routine used to set the expanded state of the specified grid row.
GetItem -- a helper function that returns the grid row object by its full label. You can find more information on this function in Accessing Microsoft PropertyGrid Rows.
IsNullValue -- a helper function that checks whether the specified value is the null value.
Using the PropertyGrid Native Members
The PropertyGrid control and its relative objects include special properties and methods that let you toggle the rows expanded state. To check or change the expanded state of a particular grid row, you can use its
Below is an example that demonstrates how you can use this property in scripts. This example differs from the previous one only in implementation of the
CollapseRow routines. To run this example successfully, you need to include the
IsNullValue functions used in the previous example in your script.
The PropertyGrid control also has the
CollapseAllGridItems methods that let you expand or collapse all grid rows at once. The following code snippet shows how you can use them in scripts: