Checking and Unchecking 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.

A tree view control can display check boxes (or custom state images) next to items, if it has the TVS_CHECKBOXES style. To check or uncheck a tree view item, use the CheckItem action of the Win32TreeView that corresponds to the tested tree view control, or the Check action of a Win32TreeViewItem object that provides access to the target item. The Win32TreeViewItem.Checked property, in its turn, lets you determine whether a particular item is checked or unchecked.

The following code demonstrates how you can check tree view items:

JavaScript, JScript

function Main ()
{
  var p, TreeView;

  // Obtain the application process and the TreeView control
  p = Sys.Process("TreeViewSample");
  TreeView = p.Window("MyWndClass", "*").Window("SysTreeView32");

  // Check and uncheck some items
  TreeView.CheckItem("|RootItem 3", true);
  TreeView.CheckItem("|RootItem 3|Node 3-2", false);
}

Python

def Main():

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

  # Check and uncheck some items
  TreeView.CheckItem("|RootItem 3", True)
  TreeView.CheckItem("|RootItem 3|Node 3-2", False)

VBScript

Sub Main
  Dim p, TreeView

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

  ' Check and uncheck some items
  Call TreeView.CheckItem("|RootItem 3", True)
  Call TreeView.CheckItem("|RootItem 3|Node 3-2", False)
End Sub

DelphiScript

procedure Main;
var p, TreeView : OleVariant;
begin
  // Obtain the application process and the TreeView control
  p := Sys.Process('TreeViewSample');
  TreeView := p.Window('MyWndClass', '*').Window('SysTreeView32');

  // Check and uncheck some items
  TreeView.CheckItem('|RootItem 3', true);
  TreeView.CheckItem('|RootItem 3|Node 3-2', false);
end;

C++Script, C#Script

function Main ()
{
  var p, TreeView;

  // Obtain the application process and the TreeView control
  p = Sys["Process"]("TreeViewSample");
  TreeView = p["Window"]("MyWndClass", "*")["Window"]("SysTreeView32");

  // Check and uncheck some items
  TreeView["CheckItem"]("|RootItem 3", true);
  TreeView["CheckItem"]("|RootItem 3|Node 3-2", false);
}

Alternatively, you can toggle the selected item’s state between checked and unchecked by simulating the Space key press. For this, you can use the Keys action:

JavaScript, JScript

function Main ()
{
  var p, TreeView;

  // Obtain the application process and the TreeView control
  p = Sys.Process("TreeViewSample");
  TreeView = p.Window("MyWndClass", "*").Window("SysTreeView32");

  // Toggle the checked state of some items
  TreeView.SelectItem("|RootItem 3");
  TreeView.Keys(" ");

  TreeView.SelectItem("|RootItem 3|Node 3-2");
  TreeView.Keys(" ");
}

Python

def Main():

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

  # Toggle the checked state of some items
  TreeView.SelectItem("|RootItem 3")
  TreeView.Keys(" ")

  TreeView.SelectItem("|RootItem 3|Node 3-2")
  TreeView.Keys(" ")

VBScript

Sub Main
  Dim p, TreeView

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

  ' Toggle the checked state of some items
  TreeView.SelectItem("|RootItem 3")
  TreeView.Keys(" ")

  TreeView.SelectItem("|RootItem 3|Node 3-2")
  TreeView.Keys(" ")
End Sub

DelphiScript

procedure Main;
var p, TreeView : OleVariant;
begin
  // Obtain the application process and the TreeView control
  p := Sys.Process('TreeViewSample');
  TreeView := p.Window('MyWndClass', '*').Window('SysTreeView32');

  // Toggle the checked state of some items
  TreeView.SelectItem('|RootItem 3');
  TreeView.Keys(' ');

  TreeView.SelectItem('|RootItem 3|Node 3-2');
  TreeView.Keys(' ');
end;

C++Script, C#Script

function Main ()
{
  var p, TreeView;

  // Obtain the application process and the TreeView control
  p = Sys["Process"]("TreeViewSample");
  TreeView = p["Window"]("MyWndClass", "*")["Window"]("SysTreeView32");

  // Toggle the checked state of some items
  TreeView["SelectItem"]("|RootItem 3");
  TreeView["Keys"](" ");

  TreeView["SelectItem"]("|RootItem 3|Node 3-2");
  TreeView["Keys"](" ");
}

See Also

Working With Tree View Controls in Desktop Windows Applications
Addressing Tree View Items in Desktop Windows Applications
Selecting Tree View Items in Desktop Windows Applications
Checking Tree View Items' State in Desktop Windows Applications
CheckItem Action (Specific to Win32TreeView Controls)
Check Action (TreeViewItem Objects)
Checked Property (TreeViewItem Objects)

Highlight search results