Selecting Tree View Items in Desktop Windows Applications

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

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.

When working with the tree view control, you quite often need to select specific items in the tree. This topic described various approaches that you can use to select items in Win32 tree view controls:

Using the Click and Select Actions

The most natural and easiest way to select a tree view item is to click on it. In tests, you can simulate mouse clicks on tree view items using various Click actions:

The Win32TreeView object actions have two parameters: Item and Shift. The Item parameter specifies the path to the tree view item that you want to click. The path consists of item captions or indexes separated by a pipeline ( | ). Note that it is possible to use wildcards (* and ?) to specify item names in the path. This path can be absolute, that is, starting from the tree root, or relative to the currently selected item (see Addressing Tree View Items in Desktop Windows Applications). The Shift parameter specifies the key or a combination of keys (Ctrl, Shift, Alt) that are pressed during the click. By default, this parameter is not used, so that no shift keys are “pressed” during the click.

The Win32TreeViewItem object actions have the Shift parameter only, since the object itself specifies the target item.

The following example demonstrates how you can use the Win32TreeView’s and Win32TreeViewItem’s Click actions in scripts:

JavaScript, JScript

function TreeViewSample()
{
 var p, TreeView;
 // Obtain the application process and the TreeView control
 p = Sys.Process("MyTestedApp");
 TreeView = p.Window("MyWndClass", "*").Window("SysTreeView32");
 
  // Click some items
 TreeView.ClickItem(0);
 TreeView.ClickItem("|Root1|Child5");
 
 TreeView.wItems.Item("Root2").Items.Item("Child4").Click();
 TreeView.wItems.Item("Root1").Click();
}

Python

def TreeViewSample():

  # Obtain the application process and the TreeView control
  p = Sys.Process("MyTestedApp")
  TreeView = p.Window("MyWndClass", "*").Window("SysTreeView32")
  
  # Click some items
  TreeView.ClickItem(0)
  TreeView.ClickItem("|Root1|Child5")

  TreeView.wItems.Item["Root2"].Items.Item["Child4"].Click()
  TreeView.wItems.Item["Root1"].Click()

VBScript

Sub TreeViewSample
  Dim p, TreeView

  ' Obtain the application process and the TreeView control
  Set p = Sys.Process("MyTestedApp")
  Set TreeView = p.Window("MyWndClass", "*").Window("SysTreeView32")

  ' Click some items
  TreeView.ClickItem(0)
  TreeView.ClickItem("|Root1|Child5")

  TreeView.wItems.Item("Root2").Items.Item("Child4").Click()
  TreeView.wItems.Item("Root1").Click()

End Sub

DelphiScript

procedure TreeViewSample;
var p, TreeView : OleVariant;
begin
  // Obtain the application process and the TreeView control
  p := Sys.Process('MyTestedApp');
  TreeView := p.Window('MyWndClass', '*').Window('SysTreeView32');
  
  // Click some items
  TreeView.ClickItem(0);
  TreeView.ClickItem('|Root1|Child5');

  TreeView.wItems.Item('Root2').Items.Item('Child4').Click();
  TreeView.wItems.Item('Root1').Click();
end;

C++Script, C#Script

function TreeViewSample()
{
  var p, TreeView;

  // Obtain the application process and the TreeView control
  p = Sys["Process"]("MyTestedApp");
  TreeView = p["Window"]("MyWndClass", "*")["Window"]("SysTreeView32");
  
  // Click some items
  TreeView["ClickItem"](0);
  TreeView["ClickItem"]("|Root1|Child5");
  
  TreeView["wItems"]["Item"]("Root2")["Items"]["Item"]("Child4")["Click"]();
  TreeView["wItems"]["Item"]("Root1")["Click"]();
}

You can also select tree view items using the Select actions -- Win32TreeView.SelectItem and Win32TreeViewItem.Select. These actions are similar to the corresponding Click actions, the only difference is that the Select actions do not move the mouse pointer:

JavaScript, JScript

function TreeViewSample()
{
 var p, TreeView;
 // Obtain the application process and the TreeView control
 p = Sys.Process("MyTestedApp");
 TreeView = p.Window("MyWndClass", "*").Window("SysTreeView32");
 
  // Click some items
 TreeView.SelectItem(0);
 TreeView.SelectItem("|Root1|Child5");
 
 TreeView.wItems.Item("Root2").Items.Item("Child4").Select();
 TreeView.wItems.Item("Root1").Select();
}

Python

def TreeViewSample():

  # Obtain the application process and the TreeView control
  p = Sys.Process("MyTestedApp")
  TreeView = p.Window("MyWndClass", "*").Window("SysTreeView32")
  
  # Click some items
  TreeView.SelectItem(0)
  TreeView.SelectItem("|Root1|Child5")

  TreeView.wItems.Item["Root2"].Items.Item["Child4"].Select()
  TreeView.wItems.Item["Root1"].Select()

VBScript

Sub TreeViewSample
  Dim p, TreeView

  ' Obtain the application process and the TreeView control
  Set p = Sys.Process("MyTestedApp")
  Set TreeView = p.Window("MyWndClass", "*").Window("SysTreeView32")

  ' Click some items
  TreeView.SelectItem(0)
  TreeView.SelectItem("|Root1|Child5")

  TreeView.wItems.Item("Root2").Items.Item("Child4").Select()
  TreeView.wItems.Item("Root1").Select()

End Sub

DelphiScript

procedure TreeViewSample;
var p, TreeView : OleVariant;
begin
  // Obtain the application process and the TreeView control
  p := Sys.Process('MyTestedApp');
  TreeView := p.Window('MyWndClass', '*').Window('SysTreeView32');
  
  // Click some items
  TreeView.SelectItem(0);
  TreeView.SelectItem('|Root1|Child5');

  TreeView.wItems.Item('Root2').Items.Item('Child4').Select();
  TreeView.wItems.Item('Root1').Select();
end;

C++Script, C#Script

function TreeViewSample()
{
  var p, TreeView;

  // Obtain the application process and the TreeView control
  p = Sys["Process"]("MyTestedApp");
  TreeView = p["Window"]("MyWndClass", "*")["Window"]("SysTreeView32");
  
  // Click some items
  TreeView["SelectItem"](0);
  TreeView["SelectItem"]("|Root1|Child5");
  
  TreeView["wItems"]["Item"]("Root2")["Items"]["Item"]("Child4")["Select"]();
  TreeView["wItems"]["Item"]("Root1")["Select"]();
}

Simulating Keyboard Shortcuts

You can navigate through a tree view control from the keyboard by using special shortcuts. For example:

  • The Up Arrow and Down Arrow keys select the previous and the next item, respectively.
  • The Home and End keys select the first and the last item, respectively.
  • The Backspace key press selects the parent item.
  • Pressing any printing character selects an item whose caption starts with this character. Pressing the same character again selects a successive item that starts with this character, and so on. This way, you can select a tree view item by “typing” its caption.
  • and so on.

To send keypresses to the tree view control, you can use the Keys action. The following code demonstrates how you can use the shortcuts to navigate in the Windows Explorer’s Folders tree:

JavaScript, JScript

function Main ()
{
  var p, w, folders;

  // Open My Computer
  WshShell.Run("explorer.exe /e, /select,C:\\", SW_SHOWNORMAL);

  // Obtain the Explorer process, window and the Folders tree
  p = Sys.Process("Explorer");
  w = p.Window("*WClass", "*");
  folders = w.Window("BaseBar").Window("ReBarWindow32").Window("SysTreeView32");

  // Expand the C drive
  folders.ExpandItem("|Desktop|My Computer|*C:*");
  // Select and expand the Program Files item
  folders.Keys("Program ");
  folders.Keys("[NumPlus]");
  // Select the Internet Explorer item
  folders.Keys("Internet ");
  // Select the Program Files item
  folders.Keys("[BS]");

  // Select the My Documents item
  folders.Keys("My ");
  // Select the Desktop item
  folders.Keys("[Home]");
}

Python

def Main ():
  
  # Open My Computer
  WshShell.Run("explorer.exe /e, /select,C:\\", SW_SHOWNORMAL)

  # Obtain the Explorer process, window and the Folders tree
  p = Sys.Process("Explorer")
  w = p.Window("*WClass", "*")
  folders = w.Window("BaseBar").Window("ReBarWindow32").Window("SysTreeView32")

  # Expand the C drive
  folders.ExpandItem("|Desktop|My Computer|*C:*")
  # Select and expand the Program Files item
  folders.Keys("Program ")
  folders.Keys("[NumPlus]")
  # Select the Internet Explorer item
  folders.Keys("Internet ")
  # Select the Program Files item
  folders.Keys("[BS]")

  # Select the My Documents item
  folders.Keys("My ")
  # Select the Desktop item
  folders.Keys("[Home]")

VBScript

Sub Main
  Dim p, w, folders

  ' Open My Computer
  Call WshShell.Run("explorer.exe /e, /select,C:\", SW_SHOWNORMAL)

  ' Obtain the Explorer process, window and the Folders tree
  Set p = Sys.Process("Explorer")
  Set w = p.Window("*WClass", "*")
  Set folders = w.Window("BaseBar").Window("ReBarWindow32").Window("SysTreeView32")

  ' Expand drive C
  folders.ExpandItem("|Desktop|My Computer|*C:*")
  ' Select and expand the Program Files item
  folders.Keys("Program ")
  folders.Keys("[NumPlus]")
  ' Select the Internet Explorer item
  folders.Keys("Internet ")
  ' Select the Program Files item
  folders.Keys("[BS]")

  ' Select the My Documents item
  folders.Keys("My ")
  ' Select the Desktop item
  folders.Keys("[Home]")
End Sub

DelphiScript

procedure Main;
var p, w, folders : OleVariant;
begin
  // Open My Computer
  WshShell.Run('explorer.exe /e, /select,C:\', SW_SHOWNORMAL);

  // Obtain the Explorer process, window and the Folders tree
  p := Sys.Process('Explorer');
  w := p.Window('*WClass', '*');
  folders := w.Window('BaseBar').Window('ReBarWindow32').Window('SysTreeView32');

  // Expand the C drive
  folders.ExpandItem('|Desktop|My Computer|*C:*');
  // Select and expand the Program Files item
  folders.Keys('Program ');
  folders.Keys('[NumPlus]');
  // Select the Internet Explorer item
  folders.Keys('Internet ');
  // Select the Program Files item
  folders.Keys('[BS]');

  // Select the My Documents item
  folders.Keys('My ');
  // Select the Desktop item
  folders.Keys('[Home]');
end;

C++Script, C#Script

function Main ()
{
  var p, w, folders;

  // Open My Computer
  WshShell["Run"]("explorer.exe /e, /select,C:\\", SW_SHOWNORMAL);

  // Obtain the Explorer process, window and the Folders tree
  p = Sys["Process"]("Explorer");
  w = p["Window"]("*WClass", "*");
  folders = w["Window"]("BaseBar")["Window"]("ReBarWindow32")["Window"]("SysTreeView32");

  // Expand the C drive
  folders["ExpandItem"]("|Desktop|My Computer|*C:*");
  // Select and expand the Program Files item
  folders["Keys"]("Program ");
  folders["Keys"]("[NumPlus]");
  // Select the Internet Explorer item
  folders["Keys"]("Internet ");
  // Select the Program Files item
  folders["Keys"]("[BS]");

  // Select the My Documents item
  folders["Keys"]("My ");
  // Select the Desktop item
  folders["Keys"]("[Home]");
}

Selecting Multiple Items

If a tree view control supports multiple selections, you can select several items at a time using Ctrl- and Shift-clicks. Clicking an item while holding the Shift key selects the range of items between the previously selected item and the clicked item. A Ctrl-click on an item toggles its selected state, that is, adds it to or removes from the current selection.

To simulate a click on an item with the Ctrl key pressed, use the Win32TreeView.ClickItem or Win32TreeViewItem.Click action with the Shift parameter set to skCtrl. If you want to Shift-click an item, set the Shift parameter to skShift:

JavaScript, JScript

function Main ()
{
  var p, TreeView;

  // Obtain the TreeView control
  p = Sys.Process("TreeViewSample");
  TreeView = p.Window("TForm1", "*").Window("TTreeView");

  // Select a range of items
  TreeView.ClickItem("|RootItem 2|SubItem 2-1");
  TreeView.ClickItem("|RootItem 2|SubItem 2-4", skShift);
  // Deselect the RootItem 2|SubItem 2-3 item
  TreeView.ClickItem("|RootItem 2|SubItem 2-3", skCtrl);
  // Add two items to the selection
  TreeView.ClickItem("|RootItem 4|SubItem 4-1", skCtrl);
  TreeView.ClickItem("|RootItem 1", skCtrl);
}

Python

def Main ():

  # Obtain the TreeView control
  p = Sys.Process("TreeViewSample")
  TreeView = p.Window("TForm1", "*").Window("TTreeView")

  # Select a range of items
  TreeView.ClickItem("|RootItem 2|SubItem 2-1")
  TreeView.ClickItem("|RootItem 2|SubItem 2-4", skShift)
  # Deselect the RootItem 2|SubItem 2-3 item
  TreeView.ClickItem("|RootItem 2|SubItem 2-3", skCtrl)
  # Add two items to the selection
  TreeView.ClickItem("|RootItem 4|SubItem 4-1", skCtrl)
  TreeView.ClickItem("|RootItem 1", skCtrl)

VBScript

Sub Main
  Dim p, TreeView

  ' Obtain the TreeView control
  Set p = Sys.Process("TreeViewSample")
  Set TreeView = p.Window("TForm1", "*").Window("TTreeView")

  ' Select a range of items
  TreeView.ClickItem("|RootItem 2|SubItem 2-1")
  Call TreeView.ClickItem("|RootItem 2|SubItem 2-4", skShift)
  ' Deselect the RootItem 2|SubItem 2-3 item
  Call TreeView.ClickItem("|RootItem 2|SubItem 2-3", skCtrl)
  ' Add two items to the selection
  Call TreeView.ClickItem("|RootItem 4|SubItem 4-1", skCtrl)
  Call TreeView.ClickItem("|RootItem 1", skCtrl)
End Sub

DelphiScript

procedure Main;
var p, TreeView : OleVariant;
begin
  // Obtain the TreeView control
  p := Sys.Process('TreeViewSample');
  TreeView := p.Window('TForm1', '*').Window('TTreeView');

  // Select a range of items
  TreeView.ClickItem('|RootItem 2|SubItem 2-1');
  TreeView.ClickItem('|RootItem 2|SubItem 2-4', skShift);
  // Deselect the RootItem 2|SubItem 2-3 item
  TreeView.ClickItem('|RootItem 2|SubItem 2-3', skCtrl);
  // Add two items to the selection
  TreeView.ClickItem('|RootItem 4|SubItem 4-1', skCtrl);
  TreeView.ClickItem('|RootItem 1', skCtrl);
end;

C++Script, C#Script

function Main ()
{
  var p, TreeView;

  // Obtain the TreeView control
  p = Sys["Process"]("TreeViewSample");
  TreeView = p["Window"]("TForm1", "*")["Window"]("TTreeView");

  // Select a range of items
  TreeView["ClickItem"]("|RootItem 2|SubItem 2-1");
  TreeView["ClickItem"]("|RootItem 2|SubItem 2-4", skShift);
  // Deselect the RootItem 2|SubItem 2-3 item
  TreeView["ClickItem"]("|RootItem 2|SubItem 2-3", skCtrl);
  // Add two items to the selection
  TreeView["ClickItem"]("|RootItem 4|SubItem 4-1", skCtrl);
  TreeView["ClickItem"]("|RootItem 1", skCtrl);
}

See Also

Working With Tree View Controls in Desktop Windows Applications
Addressing Tree View Items in Desktop Windows Applications
Checking and Unchecking Tree View Items in Desktop Windows Applications
ClickItem Action (TreeView Controls)
SelectItem Method (Specific to Win32TreeView Controls)
Click Action (TreeViewItem Objects)
Select Action (TreeViewItem Objects)

Highlight search results