Copying and Pasting Item Values in Microsoft PropertyGrid

Applies to TestComplete 15.31, last modified on March 17, 2022

When testing an application with the PropertyGrid control, you may need to copy or paste cell values to and from the clipboard. For example, you can use this to organize a data exchange between the two applications. This topic describes how you can copy and paste PropertyGrid cell values from scripts:

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.

Copying Item Values

Before copying the item value, you need to select the desired item and specify the contents to be copied or replaced. For example, to select the entire item value, you can simulate the Ctrl+A shortcut. To copy the selection to the clipboard, you can either simulate the Ctrl+C keyboard shortcut, or select the Copy command from the context menu. To simulate the shortcuts, use the Keys action applied to the grid control. To work with a popup menu you can also use the corresponding shortcuts -- the Application keypress will invoke the menu and pressing C will select the Copy command (it is the hot key for this command).

Below in an example that illustrates how you can copy PropertyGrid values.

Example

View description

JavaScript, JScript

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

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

  // Copy the "Appearance.Text" property value to the clipboard
  str = CopyValue(Grid, "Appearance.Text");
  Log.Message("Clipboard contents: " + str);

  // Copy the "Appearance.Font" property value to the clipboard
  str = CopyValue (Grid, "Appearance.Font");
  Log.Message("Clipboard contents: " + str);
}

function CopyValue (Grid, FullLabel)
{
  // Select the item
  ClickItem(Grid, FullLabel);

  // Copy the cell contents using the Ctrl+C shortcut
  Grid.Keys("^a^c");
  // You can also copy the cell value via the context menu --
  // Grid.Keys("^a[Apps]c");

  return Sys.Clipboard;
}

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")

  # Copy the "Appearance.Text" property value to the clipboard
  str = CopyValue(Grid, "Appearance.Text")
  Log.Message("Clipboard contents: " + str)

  # Copy the "Appearance.Font" property value to the clipboard
  str = CopyValue (Grid, "Appearance.Font")
  Log.Message("Clipboard contents: " + str)

def CopyValue (Grid, FullLabel):
  # Select the item
  ClickItem(Grid, FullLabel)

  # Copy the cell contents using the Ctrl+C shortcut
  Grid.Keys("^a^c")
  # You can also copy the cell value via the context menu --
  # Grid.Keys("^a[Apps]c")

  return Sys.Clipboard

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, str

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

  ' Copy the "Appearance.Text" property value to the clipboard
  str = CopyValue(Grid, "Appearance.Text")
  Log.Message("Clipboard contents: " & str)

  ' Copy the "Appearance.Font" property value to the clipboard
  str = CopyValue (Grid, "Appearance.Font")
  Log.Message("Clipboard contents: " & str)
End Sub

Function CopyValue (Grid, FullLabel)
  ' Select the item
  Call ClickItem(Grid, FullLabel)

  ' Copy the cell contents using the Ctrl+C shortcut
  Grid.Keys("^a^c")
  ' You can also copy the cell value via the context menu --
  ' Grid.Keys("^a[Apps]c")

  CopyValue = Sys.Clipboard
End Function

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

function CopyValue (Grid, FullLabel); forward;
procedure ClickItem (Grid, FullLabel); forward;

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

  // Copy the 'Appearance.Text' property value to the clipboard
  str := CopyValue(Grid, 'Appearance.Text');
  Log.Message('Clipboard contents: ' + str);

  // Copy the 'Appearance.Font' property value to the clipboard
  str := CopyValue (Grid, 'Appearance.Font');
  Log.Message('Clipboard contents: ' + str);
end;

function CopyValue (Grid, FullLabel);
begin
  // Select the item
  ClickItem(Grid, FullLabel);

  // Copy the cell contents using the Ctrl+C shortcut
  Grid.Keys('^a^c');
  // You can also copy the cell value via the context menu --
  // Grid.Keys('^a[Apps]c');

  Result := Sys.Clipboard;
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, str;

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

  // Copy the "Appearance.Text" property value to the clipboard
  str = CopyValue(Grid, "Appearance.Text");
  Log["Message"]("Clipboard contents: " + str);

  // Copy the "Appearance.Font" property value to the clipboard
  str = CopyValue (Grid, "Appearance.Font");
  Log["Message"]("Clipboard contents: " + str);
}

function CopyValue (Grid, FullLabel)
{
  // Select the item
  ClickItem(Grid, FullLabel);

  // Copy the cell contents using the Ctrl+C shortcut
  Grid["Keys"]("^a^c");
  // You can also copy the cell value via the context menu --
  // Grid["Keys"]("^a[Apps]c");

  return Sys["Clipboard"];
}

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]);
}

Pasting Item Values

Pasting the cell value from the clipboard is similar to copying it. Before performing the paste operation, you also need to focus the desired item and select its contents. To paste the value from the clipboard, you can use the Ctrl+V shortcut or the Paste command of the context menu. After you have pasted the value into the cell, you should simulate the Enter keypress to apply the changes.

The following example illustrates how you can paste values into PropertyGrid.

Example

View description

JavaScript, JScript

function Main ()
{
  var p, Grid;

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

  // Paste the "Appearance.Text" property value from the clipboard
  Sys.Clipboard = "New Text";
  PasteValue(Grid, "Appearance.Text");

  // Paste the "Appearance.Font" property value from the clipboard
  Sys.Clipboard = "Tahoma, 9pt";
  PasteValue(Grid, "Appearance.Font");
}

function PasteValue (Grid, FullLabel)
{
  // Select the item
  ClickItem (Grid, FullLabel);
  // Paste the value using the Ctrl-V shortcut
  Grid.Keys ("^a^v[Enter]");
  // You can also paste the value via the context menu --
  // Grid.Keys ("^a[Apps]p[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")

  # Paste the "Appearance.Text" property value from the clipboard
  Sys.Clipboard = "New Text"
  PasteValue(Grid, "Appearance.Text")

  # Paste the "Appearance.Font" property value from the clipboard
  Sys.Clipboard = "Tahoma, 9pt"
  PasteValue(Grid, "Appearance.Font")

def PasteValue (Grid, FullLabel):
  # Select the item
  ClickItem (Grid, FullLabel)
  # Paste the value using the Ctrl-V shortcut
  Grid.Keys ("^a^v[Enter]")
  # You can also paste the value via the context menu --
  # Grid.Keys ("^a[Apps]p[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")

  ' Paste the "Appearance.Text" property value from the clipboard
  Sys.Clipboard = "New Text"
  Call PasteValue(Grid, "Appearance.Text")

  ' Paste the "Appearance.Font" property value from the clipboard
  Sys.Clipboard = "Tahoma, 9pt"
  Call PasteValue(Grid, "Appearance.Font")
End Sub

Sub PasteValue (Grid, FullLabel)
  ' Select the item
  Call ClickItem (Grid, FullLabel)
  ' Paste the value using the Ctrl-V shortcut
  Grid.Keys("^a^v[Enter]")
  ' You can also paste the value via the context menu --
  ' Grid.Keys("^a[Apps]p[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 PasteValue (Grid, FullLabel); 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');

  // Paste the "Appearance.Text" property value from the clipboard
  Sys.Clipboard := 'New Text';
  PasteValue(Grid, 'Appearance.Text');

  // Paste the "Appearance.Font" property value from the clipboard
  Sys.Clipboard := 'Tahoma, 9pt';
  PasteValue(Grid, 'Appearance.Font');
end;

procedure PasteValue (Grid, FullLabel);
begin
  // Select the item
  ClickItem (Grid, FullLabel);
  // Paste the value using the Ctrl-V shortcut
  Grid.Keys ('^a^v[Enter]');
  // You can also paste the value via the context menu --
  // Grid.Keys ('^a[Apps]p[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");

  // Paste the "Appearance.Text" property value from the clipboard
  Sys["Clipboard"] = "New Text";
  PasteValue(Grid, "Appearance.Text");

  // Paste the "Appearance.Font" property value from the clipboard
  Sys["Clipboard"] = "Tahoma, 9pt";
  PasteValue(Grid, "Appearance.Font");
}

function PasteValue (Grid, FullLabel)
{
  // Select the item
  ClickItem (Grid, FullLabel);
  // Paste the value using the Ctrl-V shortcut
  Grid["Keys"]("^a^v[Enter]");
  // You can also paste the value via the context menu --
  // Grid["Keys"]("^a[Apps]p[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]);
}

See Also

Working With Microsoft PropertyGrid
Accessing Items in Microsoft PropertyGrid
Obtaining and Setting Item Values in Microsoft PropertyGrid
Selecting Items in Microsoft PropertyGrid
Working With In-place Editors in Microsoft PropertyGrid

Highlight search results