One of the actions you will perform quite often while testing an application with the PropertyGrid control is selecting grid cells. For example, you may need to select a cell in order to enter the new property value.
Below I will describe two approaches that can be used to select PropertyGrid cells: simulating mouse clicks on cells and using the PropertyGrid.SelectedGridItem
property. The approach that uses the internal property is easier to implement, but does not involve any user interaction with the control. The approach that simulates a mouse click on the cell requires much more coding, but it will make the grid response to the click, for example, fire event handlers attached to its Click
event. In your tests, you can the any of these approaches that suits your needs.
Simulating Cell Clicks
To simulate a mouse click on a PropertyGrid row, you can use the Click
action applied to the PropertyGrid control. This action is added to all onscreen objects by TestComplete. It requires you to know the coordinates of the desired cell within the grid.
You can determine the desired row coordinates via the special AccessibilityObject
property which is common to most controls in the .NET Framework. This property holds various accessible information about the object, including its screen coordinates. Using the ScreenToWindow
method, you can convert the screen-related coordinates of the grid row to its grid-related coordinates and pass them to the Click
action.
However, this is how you can get the coordinates of the whole grid row, but you need to know the property value cell coordinates. You can calculate them based on the row coordinates, using some internal methods of the PropertyGrid view object (I have already mentioned this object in the previous article, Accessing Microsoft PropertyGrid Rows):
GetLabelWidth()
-- returns the width of the left column (containing property labels);
GetValueWidth()
-- returns the width of the right column (containing property values).
That is, to get the horizontal coordinate of the property value cell center, you need to subtract the half of the value column width from the rows right edge coordinate.
The example below illustrates the described approach. It simulates a click in the center of the row value cell specified by its label. This example consists of the following routines:
Main
is the main script routine. It calls ClickCell
to click the Layout.Size property cell.
- The
ClickCell
routine simulates a mouse click on the PropertyGrid cell specified by its full label. It obtains the object corresponding to the specified row using the GetItem
function, then calculates the rows value cell coordinates and makes a click on it. At that, if the row is not currently visible because it is situated out of the grid view, the grid is scrolled so that the desired row becomes visible.
- The
GetCell
function returns the PropertyGrid row by its full label, for example, Layout.Size. You can find more information on this function in Accessing Microsoft PropertyGrid Rows.
IsDisplayed
is a helper function that checks whether the specified grid row is displayed. It returns True if all parent rows of the specified row are expanded, and False otherwise.
Using the PropertyGrid.SelectedGridItem Property
The PropertyGrid control has the SelectedGridItem
property that lets you obtain the selected row or directly specify it. This property can be used to select rows that are currently visible in the grid as well as those that are currently out of the grid view. In the latter case, the grid is scrolled so that the selected row becomes visible. However, if you use this property to select a hidden row, that is the row whose parent row is collapsed, the exception will occur.
Below is an example that illustrates how you can use this property to select a grid row. The example consists of two routines: Main
and SelectItem
. The latter uses the SelectedGridItem
property to select the specified grid row. To get the row object by its full label, it calls the GetItem
function, whose code you can find in the previous example.