Obtaining and Setting Item Values in Microsoft PropertyGrid

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

This topic describes the various approaches that you can use to obtain and change item values displayed in the PropertyGrid control:

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.

Obtaining Item Values

To get the value of a specific PropertyGrid item, you can use the wValue property of the MicrosoftPropertyGrid or PropertyGridItemGroup objects. The MicrosoftPropertyGrid.wValue property provides access to values of top-level items, the PropertyGridItemGroup.wValue - to values of child items. Note that the wValue property returns the “native” .NET object corresponding to the item value. Some “simple” values, for example, Integer, Boolean and others, are OLE-compatible and thus can be used in scripts directly. Others, such as String, Decimal, DateTime and enumeration values are made OLE-compatible via the OleValue property added to these objects by TestComplete. To work with complex object values, such as System.Drawing.Font, use their internal properties and methods.

Below is an example that demonstrates how you can obtain values from the PropertyGrid:

JavaScript

function Main ()
{
  var p, Grid, Value, Str;

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

  // Switch to the "Alphabetical" view
  Grid.PropertySort = "Alphabetical";

  // Post item values to the log
  for (let i=0; i<Grid.wItemCount; i++)
  {
    Value = Grid.wValue(i);
    if (strictEqual(Value, null))
      Str = ""
    else if (aqObject.IsSupported(Value, "OleValue"))
      Str = Value.OleValue.toString()
    else
      Str = Value.ToString().OleValue;
    Log.Message(Grid.wLabel(i) + ": " + Str);
  }
}

JScript

function Main ()
{
  var p, Grid, i, Value, Str;

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

  // Switch to the "Alphabetical" view
  Grid.PropertySort = "Alphabetical";

  // Post item values to the log
  for (i=0; i<Grid.wItemCount; i++)
  {
    Value = Grid.wValue(i);
    if (Value == null)
      Str = ""
    else if (aqObject.IsSupported(Value, "OleValue"))
      Str = Value.OleValue.toString()
    else
      Str = Value.ToString().OleValue;
    Log.Message(Grid.wLabel(i) + ": " + Str);
  }
}

Python

def Main ():

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

  # Switch to the "Alphabetical" view
  Grid.PropertySort = "Alphabetical";

  # Post item values to the log
  for i in range(0, Grid.wItemCount-1):
    Value = Grid.wValue(i);
    if (Value == null):
      Str = ""
    elif (aqObject.IsSupported(Value, "OleValue")):
      Str = Value.OleValue.toString()
    else:
      Str = Value.ToString().OleValue;
    Log.Message(Grid.wLabel[i] + ": " + Str);

VBScript

Sub Main
  Dim p, Grid, i, Value, Str

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

  ' Switch to the "Alphabetical" view
  Grid.PropertySort = "Alphabetical"

  ' Post item values to the log
  For i=0 To Grid.wItemCount-1
    Set Value = Grid.wValue(i)
    If Value Is Nothing Then
      Str = ""
    ElseIf aqObject.IsSupported(Value, "OleValue") Then
      Str = CStr(Value.OleValue)
    Else
      Str = Value.ToString.OleValue
    End If
    Log.Message(Grid.wLabel(i) & ": " & Str)
  Next
End Sub

DelphiScript

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

  // Switch to the 'Alphabetical' view
  Grid.PropertySort := 'Alphabetical';

  // Post item values to the log
  for i := 0 to Grid.wItemCount-1 do
  begin
    Value := Grid.wValue[i];
    if Value = nil then
      Str := ''
    else if aqObject.IsSupported(Value, 'OleValue') then
      Str := aqConvert.VarToStr(Value.OleValue)
    else
      Str := Value.ToString.OleValue;
    Log.Message(Grid.wLabel[i] + ': ' + Str)
  end
end;

C++Script, C#Script

function Main ()
{
  var p, Grid, i, Value, Str;

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

  // Switch to the "Alphabetical" view
  Grid["PropertySort"] = "Alphabetical";

  // Post item values to the log
  for (i=0; i<Grid["wItemCount"]; i++)
  {
    Value = Grid["wValue"](i);
    if (Value == null)
      Str = ""
    else if (aqObject["IsSupported"](Value, "OleValue"))
      Str = Value["OleValue"]["toString"]()
    else
      Str = Value["ToString"]()["OleValue"];
    Log["Message"](Grid["wLabel"](i) + ": " + Str);
  }
}

Setting Item Values

There are two approaches that you can be used to modify the PropertyGrid item values:

  • Simulating user actions over the item’s in-place editor, for example, “typing” the value into the grid.
  • Assigning the new value programmatically using the wValue property or internal properties of the PropertyGrid control.

Detailed information and script samples for both approaches are provided below. These approaches work well for most types of in-place editors. However, if the PropertyGrid the tested application uses specific in-place editors (for example, dropdown lists), you may need to work with them in a custom way. For more information on how to work with specific editors, see Working With In-place Editors in Microsoft PropertyGrid.

Simulating User Input

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, “Appearance.BackColor”, “Layout.Anchor’ and others. To enter text into the grid, you can use the Keys action. The general sequence of actions should include selecting the cell, typing the new value into the cell and pressing Enter to apply the changes. The InputValue routine in the following example illustrates how this can be done.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid;

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

  // Modify some values
  InputValue (Grid, "Appearance.Text", "New Text");
  InputValue (Grid, "Appearance.Font", "Tahoma, 9pt");
  InputValue (Grid, "Layout.Location", "0, 0");
}

function InputValue (Grid, FullLabel, Text)
{
  ClickItem (Grid, FullLabel);
  Grid.Keys("^a[Del]" + Text + "[Enter]");
}

function ClickItem (Grid, FullLabel)
{
  var ItemGroup, Labels, i;

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

  // Walk down the items hierarchy
  ItemGroup = Grid;
  for (i=0; i<Labels.length-1; i++)
    ItemGroup = ItemGroup.wItems(Labels[i]);

  ItemGroup.ClickValue(Labels[i]);
}

Python

def Main ():

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

  # Modify some values
  InputValue (Grid, "Appearance.Text", "New Text");
  InputValue (Grid, "Appearance.Font", "Tahoma, 9pt");
  InputValue (Grid, "Layout.Location", "0, 0");

def InputValue (Grid, FullLabel, Text):
  ClickItem (Grid, FullLabel);
  Grid.Keys("^a[Del]" + Text + "[Enter]");

def ClickItem (Grid, FullLabel):

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

  # Walk down the items hierarchy
  ItemGroup = Grid;
  for i in range(0, Labels.length-1):
    ItemGroup = ItemGroup.wItems[Labels[i]];

  ItemGroup.ClickValue[Labels[i]];

VBScript

Sub Main
  Dim p, Grid

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

  ' Modify some values
  Call InputValue (Grid, "Appearance.Text", "New Text")
  Call InputValue (Grid, "Appearance.Font", "Tahoma, 9pt")
  Call InputValue (Grid, "Layout.Location", "0, 0")
End Sub

Sub InputValue (Grid, FullLabel, Text)
  Call ClickItem (Grid, FullLabel)
  Grid.Keys("^a[Del]" & Text & "[Enter]")
End Sub

Sub ClickItem (Grid, FullLabel)
  Dim ItemGroup, Labels, i

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

  ' Access and select the item via its parent items
  Set ItemGroup = Grid
  For i = 0 To UBound(Labels)-1
    Set ItemGroup = ItemGroup.wItems(Labels(i))
  Next

  ItemGroup.ClickValue(Labels(i))
End Sub

DelphiScript

procedure InputValue (Grid, FullLabel, Text); forward;
procedure ClickItem (Grid, FullLabel); forward;

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

  // Modify some values
  InputValue (Grid, 'Appearance.Text', 'New Text');
  InputValue (Grid, 'Appearance.Font', 'Tahoma, 9pt');
  InputValue (Grid, 'Layout.Location', '0, 0');
end;

procedure InputValue (Grid, FullLabel, Text);
begin
  ClickItem (Grid, FullLabel);
  Grid.Keys('^a[Del]' + Text + '[Enter]');
end;

procedure ClickItem (Grid, FullLabel);
var ItemGroup, nLabels, i : OleVariant;
begin
  // Split the full label into parts
  aqString.ListSeparator := '.';
  nLabels := aqString.GetListLength (FullLabel);

  // Access and select the item via its parent items
  ItemGroup := Grid;
  for i := 0 to nLabels-2 do
    ItemGroup := ItemGroup.wItems[ aqString.GetListItem(FullLabel, i) ];

  ItemGroup.ClickValue( aqString.GetListItem(FullLabel, i) );
end;

C++Script, C#Script

function Main ()
{
  var p, Grid;

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

  // Modify some values
  InputValue (Grid, "Appearance.Text", "New Text");
  InputValue (Grid, "Appearance.Font", "Tahoma, 9pt");
  InputValue (Grid, "Layout.Location", "0, 0");
}

function InputValue (Grid, FullLabel, Text)
{
  ClickItem (Grid, FullLabel);
  Grid["Keys"]("^a[Del]" + Text + "[Enter]");
}

function ClickItem (Grid, FullLabel)
{
  var ItemGroup, Labels, i;

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

  // Walk down the items hierarchy
  ItemGroup = Grid;
  for (i=0; i<Labels["length"]-1; i++)
    ItemGroup = ItemGroup["wItems"](Labels[i]);

  ItemGroup["ClickValue"](Labels[i]);
}

Note: A possible alternative to typing data into the cell is to paste it from the clipboard. To learn how you can simulate the corresponding user actions, see Copying and Pasting Item Values in Microsoft PropertyGrid.
Assigning the New Value Programmatically

Another way to modify grid cell values is to use the wValue property of the MicrosoftPropertyGrid or PropertyGridItemGroup objects. This property is read-write, so it can be used to obtain grid cell values as well as to change them. Note, that to assign values to object properties, such as Font or Color, you will need to create an instance of the corresponding .NET class. You can do this via the dotNET object (see Calling Functions From .NET Assemblies for details).

The example below demonstrates how you can use the wValue property to modify the PropertyGrid item values from scripts:

Example

View description

JavaScript

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

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

  // Modify some item values
  Grid.wItems("Appearance").$set("wValue", "Text", "New Text");
  Grid.wItems("Layout").wItems("Size").$set("wValue", "Width", 500);
  Grid.wItems("Window Style").$set("wValue", "MaximizeBox", false);
  newFont = CreateFont ("Tahoma", 9);
  Grid.wItems("Appearance").$set("wValue", "Font", newFont);
}

function CreateFont (Name, Size)
{
  return Sys.Process("PropertyGridSample").AppDomain("PropertyGridSample.exe").dotNET.System_Drawing.Font.zctor_13(Name, Size);
}

JScript

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

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

  // Modify some item values
  Grid.wItems("Appearance").wValue("Text") = "New Text";
  Grid.wItems("Layout").wItems("Size").wValue("Width") = 500;
  Grid.wItems("Window Style").wValue("MaximizeBox") = false;
  newFont = CreateFont ("Tahoma", 9);
  Grid.wItems("Appearance").wValue("Font") = newFont;
}

function CreateFont (Name, Size)
{
  return Sys.Process("PropertyGridSample").AppDomain("PropertyGridSample.exe").dotNET.System_Drawing.Font.zctor_13(Name, Size);
}

Python

def Main ():

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

  # Modify some item values
  Grid.wItems("Appearance").wValue("Text") = "New Text";
  Grid.wItems("Layout").wItems("Size").wValue("Width") = 500;
  Grid.wItems("Window Style").wValue("MaximizeBox") = False;
  newFont = CreateFont ("Tahoma", 9);
  Grid.wItems("Appearance").wValue("Font") = newFont;

def CreateFont (Name, Size):
  return Sys.Process("PropertyGridSample").AppDomain("PropertyGridSample.exe").dotNET.System_Drawing.Font.zctor_13(Name, Size);

VBScript

Sub Main
  Dim p, Grid, newFont

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

  ' Modify some item values
  Grid.wItems("Appearance").wValue("Text") = "New Text"
  Grid.wItems("Layout").wItems("Size").wValue("Width") = CLng(300)
  Grid.wItems("Window Style").wValue("MaximizeBox") = False
  Set newFont = CreateFont ("Tahoma", 9)
  Grid.wItems("Appearance").wValue("Font") = newFont
End Sub

Function CreateFont (Name, Size)
  Set CreateFont = Sys.Process("PropertyGridSample").AppDomain("PropertyGridSample.exe").dotNET.System_Drawing.Font.zctor_13(Name, Size)
End Function

DelphiScript

function CreateFont (Name, Size);
begin
  Result := Sys.Process('PropertyGridSample').AppDomain('PropertyGridSample.exe').dotNET.System_Drawing.Font.zctor_13(Name, Size);
end;

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

  // Modify some item values
  Grid.wItems['Appearance'].wValue('Text') := 'New Text';
  Grid.wItems['Layout'].wItems['Size'].wValue('Width') := 300;
  Grid.wItems['Window Style'].wValue('MaximizeBox') := false;
  newFont := CreateFont ('Tahoma', 9);
  Grid.wItems['Appearance'].wValue('Font') := newFont;
end;

C++Script, C#Script

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

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

  // Modify some item values
  Grid["wItems"]("Appearance")["wValue"]("Text") = "New Text";
  Grid["wItems"]("Layout")["wItems"]("Size")["wValue"]("Width") = 500;
  Grid["wItems"]("Window Style")["wValue"]("MaximizeBox") = false;
  newFont = CreateFont ("Tahoma", 9);
  Grid["wItems"]("Appearance")["wValue"]("Font") = newFont;
}

function CreateFont (Name, Size)
{
  return Sys["Process"]("PropertyGridSample")["AppDomain"]("PropertyGridSample.exe")["dotNET"]["System_Drawing"]["Font"]["zctor_13"](Name, Size);
}

See Also

Working With Microsoft PropertyGrid
Selecting Items in Microsoft PropertyGrid
Copying and Pasting Item Values in Microsoft PropertyGrid
Working With In-place Editors in Microsoft PropertyGrid
wLabel Property (Specific to PropertyGrid Controls)
wValue Property (Specific to PropertyGrid Controls)

Highlight search results