Getting Selected Tree View Items

Applies to TestComplete 14.93, last modified on October 12, 2021

When working with tree view controls, you may need to know which items are selected in it, in order to process them in a special way.

While testing tree view controls, you can use specific properties and methods of the corresponding program object to perform certain actions and obtain data stored in controls. You can call these methods and properties from your keyword tests, as well as from scripts. This topic describes how to work with the needed properties and methods from your scripts. However, when testing a 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.

If the tree view control in the tested application works in the single-selection mode (that is, only one item can be selected at a time), you can determine the selected item using the Win32ToolBar.wSelected property. This property returns the full “path” to the selected item, which includes the item’s caption and captions of all of its parent items. See Getting the Current Tree View Item.

If the tree view control supports multiple selection, the task is a little more complicated, since the Win32TreeView object does not provide any properties or methods that would return the list of all selected tree view items. However, a Win32TreeViewItem object, which provides access to individual tree view items, has the Selected property that indicates the item’s selected state. So, you can obtain the list of selected items by iterating through all tree view items and checking the Selected property of each item. Below is an example that demonstrates how you can do this.

This example contains the GetSelectedItems function that returns the list of selected tree view items. The function has two parameters:

  • TreeView - A Win32TreeView object that corresponds to the tested tree view control.
  • Separator - A character or string that will be used to separate item paths in the returned list. For example, you can use the new line character (“\r\n” in JavaScript, JScript, Python, C#Script and C++Script, vbNewLine constant in VBScript, #13#10 in DelphiScript).

The GetSelectedItems function returns the string containing the selected items’“paths” separated with Separator characters.

JavaScript

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

  // Obtain the TreeView control
  p = Sys.Process("Project1");
  TreeView = p.Window("TForm2", "Form2", 1).Window("TTreeView", "", 1);

  // Get the list of the selected items
  str = GetSelectedItems (TreeView, "\r\n");
  Log.Message (str);
}

function GetSelectedItems (TreeView, Separator)
{
  return GetSelectedItemsInternal (TreeView.wItems, "", Separator);
}

function GetSelectedItemsInternal (TreeViewItems, Path, Separator)
{
  var Item, str;

  str = "";

  // Iterate through the tree view items
  for (let i=0; i<TreeViewItems.Count; i++)
  {
    // Get an item
    Item = TreeViewItems.Item(i);
    // If the item is selected, save its "path" to the list
    if (Item.Selected)
      str += Path + "|" + Item.Text + Separator;
    // If the item has child items, process them as well
    if (!strictEqual(Item.Items, null))
      str += GetSelectedItemsInternal(Item.Items, Path + "|" + Item.Text, Separator);
  }

  return str;
}

JScript

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

  // Obtain the TreeView control
  p = Sys.Process("Project1");
  TreeView = p.Window("TForm2", "Form2", 1).Window("TTreeView", "", 1);

  // Get the list of the selected items
  str = GetSelectedItems (TreeView, "\r\n");
  Log.Message (str);
}

function GetSelectedItems (TreeView, Separator)
{
  return GetSelectedItemsInternal (TreeView.wItems, "", Separator);
}

function GetSelectedItemsInternal (TreeViewItems, Path, Separator)
{
  var Item, i, str;

  str = "";

  // Iterate through the tree view items
  for (i=0; i<TreeViewItems.Count; i++)
  {
    // Get an item
    Item = TreeViewItems.Item(i);
    // If the item is selected, save its "path" to the list
    if (Item.Selected)
      str += Path + "|" + Item.Text + Separator;
    // If the item has child items, process them as well
    if (Item.Items != null)
      str += GetSelectedItemsInternal(Item.Items, Path + "|" + Item.Text, Separator);
  }

  return str;
}

Python

def Main():

  # Obtain the TreeView control
  p = Sys.Process("Project1")
  TreeView = p.Window("TForm2", "Form2", 1).Window("TTreeView", "", 1)

  # Get the list of the selected items
  str = GetSelectedItems (TreeView, "\r\n")
  Log.Message (str)

def GetSelectedItems (TreeView, Separator):
  return GetSelectedItemsInternal (TreeView.wItems, "", Separator)

def GetSelectedItemsInternal (TreeViewItems, Path, Separator):

  str = ""

  # Iterate through the tree view items
  for i in range(0, TreeViewItems.Count-1):
    # Get an item
    Item = TreeViewItems.Item[i]
    # If the item is selected, save its "path" to the list
    if (Item.Selected):
      str += Path + "|" + Item.Text + Separator
    # If the item has child items, process them as well
    if (Item.Items != None):
      str += GetSelectedItemsInternal(Item.Items, Path + "|" + Item.Text, Separator)
      
  return str

VBScript

Sub Main
  Dim p, TreeView, str

  ' Obtain the TreeView control
  Set p = Sys.Process("Project1")
  Set TreeView = p.Window("TForm2", "Form2", 1).Window("TTreeView", "", 1)

  ' Get the list of the selected items
  str = GetSelectedItems (TreeView, vbNewLine)
  Log.Message (str)
End Sub

Function GetSelectedItems (TreeView, Separator)
  GetSelectedItems = GetSelectedItemsInternal (TreeView.wItems, "", Separator)
End Function

Function GetSelectedItemsInternal (TreeViewItems, Path, Separator)
  Dim Item, i, str

  str = ""

  ' Iterate through the tree view items
  For i = 0 To TreeViewItems.Count-1
    ' Get an item
    Set Item = TreeViewItems.Item(i)
    ' If the item is selected, save its "path" to the list
    If Item.Selected Then
      str = str & Path & "|" & Item.Text & Separator
    End If
    ' If the item has child items, process them as well
    If Not (Item.Items Is Nothing) Then
      str = str & GetSelectedItemsInternal(Item.Items, Path & "|" & Item.Text, Separator)
    End If
  Next

  GetSelectedItemsInternal = str
End Function

DelphiScript

function GetSelectedItems (TreeView, Separator); forward;
function GetSelectedItemsInternal (TreeViewItems, Path, Separator); forward;

procedure Main;
var p, TreeView, str : OleVariant;
begin
  // Obtain the TreeView control
  p := Sys.Process('Project1');
  TreeView := p.Window('TForm2', 'Form2').Window('TTreeView', '', 1);

  // Get the list of the selected items
  str := GetSelectedItems (TreeView, #13#10);
  Log.Message (str);
end;

function GetSelectedItems (TreeView, Separator);
begin
  Result := GetSelectedItemsInternal (TreeView.wItems, '', Separator);
end;

function GetSelectedItemsInternal (TreeViewItems, Path, Separator);
var Item, i, str : oleVariant;
begin
  str := '';

  // Iterate through the tree view items
  for i := 0 to TreeViewItems.Count-1 do
  begin
    // Get an item
    Item := TreeViewItems.Item[i];
    // If the item is selected, save its "path" to the list
    if Item.Selected then
      str := str + Path + '|' + Item.Text + Separator;
    // If the item has child items, process them as well
    if Item.Items <> nil then
      str := str + GetSelectedItemsInternal(Item.Items, Path + '|' + Item.Text, Separator);
  end;

  Result := str;
end;

C++Script, C#Script

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

  // Obtain the TreeView control
  p = Sys["Process"]("Project1");
  TreeView = p["Window"]("TForm2", "Form2", 1)["Window"]("TTreeView", "", 1);

  // Get the list of the selected items
  str = GetSelectedItems (TreeView, "\r\n");
  Log["Message"](str);
}

function GetSelectedItems (TreeView, Separator)
{
  return GetSelectedItemsInternal (TreeView.wItems, "", Separator);
}

function GetSelectedItemsInternal (TreeViewItems, Path, Separator)
{
  var Item, i, str;

  str = "";

  // Iterate through the tree view items
  for (i=0; i<TreeViewItems["Count"]; i++)
  {
    // Get an item
    Item = TreeViewItems["Item"](i);
    // If the item is selected, save its "path" to the list
    if (Item["Selected"])
      str += Path + "|" + Item["Text"] + Separator;
    // If the item has child items, process them as well
    if (Item["Items"] != null)
      str += GetSelectedItemsInternal(Item["Items"], Path + "|" + Item["Text"], Separator);
  }

  return str;
}

See Also

Working With Tree View Controls
wSelection Property (Specific to Win32TreeView Controls)
Selected Property (TreeViewItem Objects)
Getting the Current Tree View Item

Highlight search results