Getting and Setting PropertyGrid Cell Values

  März 20, 2007

When testing an application with the PropertyGrid control, you may need to get values displayed by the grid as well as to modify them. In this article, I will explain how you can obtain and modify PropertyGrid item values from TestComplete scripts.

Getting Cell Values

To obtain the value of a particular property displayed in the PropertyGrid, you can use the corresponding grid item’s PropertyValue property. Note that this property returns the ”native” .NET object that corresponds to the object’s property value. Some “simple” values, for example, Integer, Boolean and others are OLE-compatible and can be used in scripts directly. To get OLE-compatible values of String, Decimal, DateTime and enumeration values, you should use the OleValue property added to these objects by TestComplete. To work with complex object values, use their own internal properties and methods.

If you want to get the string representation of a particular .NET value, you should cast it to an OLE-compatible string type. The easiest way to do this is to use the object.ToString().OleValue statement.

The example below demonstrates how you can obtain a PropertyGrid cell value as a .NET object and in string form. The example contains the following routines:

  • Main -- The script’s main routine. It obtains the scripting object corresponding to the grid, obtains the value of the “Layout.Size” property in two ways, and posts it to the test log.
  • GetValue -- Returns the grid cell value as a .NET object.
  • GetTextValue -- Returns the grid cell value in an OLE-compatible string format.
  • GetItem -- Returns the grid row by its full label. You can find more information on this routine here: Accessing Microsoft PropertyGrid Rows.
  • IsNullValue – A helper routine that checks whether the specified value is the null value.

Setting Cell Values

There are two approaches that you can use to modify the grid item value:

  • Using the grid item’s PropertyValue property.
  • Simulating user actions over the cell editor.

You may use any of these approaches. Note however, that modifying the cell value programmatically by using the PropertyValue property does not involve any user interaction with controls and thus will not trigger the corresponding events. You may find that simulating user actions over the grid is more suitable to your needs.

Using the Grid Item’s PropertyValue Property

The grid item’s PropertyValue property can be used to obtain the cell value as well as to modify it. You can change the grid item value by assigning the desired value to its PropertyValue property.

Note that to assign values to object properties, such as Font or Color, you should create an instance of the corresponding .NET class. You can do this using TestComplete’s dotNET object that allows you to call functions from .NET assemblies. To be able to call a constructor of a particular class, you need to add the assembly holding this class to the project’s CLR Bridge options (this is required for all assemblies except for the mscorlib.dll, since it is always available via the dotNET object). To do this:

  1. Double-click the project node in the Project Explorer.
  2. Switch to the Properties tab.
  3. Select the CLR Bridge options group.
  4. To add an assembly that is registered in the GAC, click Browse GAC and select the needed assembly from the subsequent dialog. To add an assembly from an arbitrary file, click Browse Files and browse for the needed assembly file.
  5. Select the File | Save All item to save changes.

After that you will be able to call routines from the added assembly in your scripts.

After changing the grid item’s value using the PropertyValue property, you need to refresh the grid control. You can do this using the grid’s Refresh method. However, since the method name coincides with TestComplete’s Refresh method name, the grid’s Refresh method is moved to the NativeClrObject namespace, and you should call it using the grid.NativeClrObject.Refresh notation.

The following example demonstrates how you can use the PropertyValue property to modify the PropertyGrid item values from scripts. It changes string, boolean and Color type values; and requires that you add the System.Drawing.dll assembly to the project’s CLR Bridge options, as described above. The example includes the following routines:

  • Main -- The main script routine. It obtains the scripting object corresponding to the grid control and modifies the values of the properties displayed in it.
  • SetValue -- A new value to the grid item specified by its full label. To obtain the grid row object, it uses the GetItem routine from the previous example.
  • CreateColor -- Creates an instance of the System.Drawing.Color structure specified by the color name.

Simulating User Actions Over the In-place Editor

Instead of using internal properties and methods of the PropertyGrid control and its objects to modify the cell value, you can simulate the user actions that, for example, type the value into the cell, select the value from the list, and so on. Note, that before changing the grid item value in this way, you need to locate it in the grid and select it. I have described how you can do this in the previous articles -- Accessing Microsoft PropertyGrid Rows. and Selecting Cells in Microsoft PropertyGrid.
To change the cell value, you can “type” the desired value into it. This way, you can change text values as well as values of those types that can be converted from a string, for example, BackColor, Anchor and others. This way you can also “select” items from dropdown list boxes, because they support the automatic completion feature.

You can use TestComplete’s Keys action, which is applied to the grid control, to enter text into the cell. The general sequence of actions should include selecting the cell, typing the new value into the cell and pressing Enter to apply the changes.

Below is an example that illustrates how this can be done. The example contains the following routines:

  • Main -- The main routine. It obtains the scripting object corresponding to the grid control and calls TypeToCell to modify the “Appearance.Text” and “Behavior.AllowDrop” property values.
  • TypeToCell -- Enters a new value into the specified grid cell. It clicks the cell, deletes its previous contents and types the new value. To simulate a cell click, it uses the helper ClickCell routine; you can find its code and description here: Selecting Cells in Microsoft PropertyGrid.