Getting the Focused Item in Microsoft PropertyGrid

Applies to TestComplete 15.47, last modified on January 20, 2023

When testing an application that uses Microsoft PropertyGrid controls, you may need to know which item is currently focused. You can easily determine the focused item using the SelectedGridItem property. This is an internal property of the PropertyGrid control, and in order for TestComplete to be able to access this property, the .NET Application Support plugin must be installed and enabled.

JavaScript

function Main ()
{
  let p, Grid, Item;

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

  // Get the selected item
  Item = Grid.SelectedGridItem;

  Log.Message ("Selected item: " + Item.Label.OleValue);
  if (!strictEqual(Item.Value, null))
    Log.Message ("Item value: " + Item.Value.ToString().OleValue)
  else
    Log.Message ("Item value: null");
}

JScript

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

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

  // Get the selected item
  Item = Grid.SelectedGridItem;

  Log.Message ("Selected item: " + Item.Label.OleValue);
  if (Item.Value != null)
    Log.Message ("Item value: " + Item.Value.ToString().OleValue)
  else
    Log.Message ("Item value: null");
}

Python

def Main ():

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

  # Get the selected item
  Item = Grid.SelectedGridItem

  Log.Message ("Selected item: " + Item.Label.OleValue)
  if (Item.Value != None):
    Log.Message ("Item value: " + Item.Value.ToString().OleValue)
  else:
    Log.Message ("Item value: None")

VBScript

Sub Main
  Dim p, Grid, Item

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

  ' Get the selected item
  Set Item = Grid.SelectedGridItem

  Log.Message ("Selected item: " & Item.Label.OleValue)
  If Not (Item.Value Is Nothing) Then 
    Log.Message ("Item value: " & Item.Value.ToString.OleValue)
  Else
    Log.Message ("Item value: null")
  End If
End Sub

DelphiScript

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

  // Get the selected item
  Item := Grid.SelectedGridItem;

  Log.Message ('Selected item: ' + Item.Label.OleValue);
  if Item.Value <> nil then
    Log.Message ('Item value: ' + Item.Value.ToString.OleValue)
  else
    Log.Message ('Item value: nil');
end;

C++Script, C#Script

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

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

  // Get the selected item
  Item = Grid["SelectedGridItem"];

  Log["Message"]("Selected item: " + Item["Label"]["OleValue"]);
  if (Item["Value"] != null)
    Log["Message"]("Item value: " + Item["Value"]["ToString"]()["OleValue"])
  else
    Log["Message"]("Item value: null");
}

See Also

Working With Microsoft PropertyGrid
Accessing Items in Microsoft PropertyGrid
Selecting Items in Microsoft PropertyGrid
Obtaining and Setting Item Values in Microsoft PropertyGrid

Highlight search results