Expanding and Collapsing Tree View Items in Desktop Windows Applications

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

Tree view controls display a hierarchical list of items. Each tree view item can have a number of child items, which are indented below this item. Child items are displayed when their parent item is expanded and hidden when the item is collapsed.

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.

TestComplete can record and play back expanding or collapsing of tree view items. This topic describes various approaches that let you expand and collapse items in standard Win32 tree view controls.

Using the ExpandItem and CollapseItem Actions

To expand and collapse items in a tree view control, you can use the ExpandItem and CollapseItem actions of a Win32TreeView object that corresponds to the tested tree view, or the Expand and Collapse actions of a Win32TreeViewItem object that provides access to the needed tree view item. The Win32TreeView.wExpanded or Win32TreeViewItem.Expanded properties let you determine whether a specific tree view item is expanded or collapsed.

The following example demonstrates how you can use the mentioned actions in scripts. It expands and collapses some items 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:\Program Files\Internet Explorer\ folder
  folders.ExpandItem("|Desktop|My Computer|*C:*");
  folders.ExpandItem("|Desktop|My Computer|*C:*|Program Files");
  folders.ExpandItem("|Desktop|My Computer|*C:*|Program Files|Internet Explorer");

  aqUtils.Delay (500);

  // Collapse My Computer
  item = folders.wItems.Item("Desktop").Items.Item("My Computer");
  item.Collapse();
}

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:\Program Files\Internet Explorer\ folder
  folders.ExpandItem("|Desktop|My Computer|*C:*")
  folders.ExpandItem("|Desktop|My Computer|*C:*|Program Files")
  folders.ExpandItem("|Desktop|My Computer|*C:*|Program Files|Internet Explorer")

  aqUtils.Delay (500)

  # Collapse My Computer
  item = folders.wItems.Item("Desktop").Items.Item["My Computer"]
  item.Collapse()

VBScript

Sub Main
  Dim p, w, folders, item

  ' 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 the C:\Program Files\Internet Explorer\ folder
  folders.ExpandItem("|Desktop|My Computer|*C:*")
  folders.ExpandItem("|Desktop|My Computer|*C:*|Program Files")
  folders.ExpandItem("|Desktop|My Computer|*C:*|Program Files|Internet Explorer")

  aqUtils.Delay (500)

  ' Collapse My Computer
  Set item = folders.wItems.Item("Desktop").Items.Item("My Computer")
  item.Collapse
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:\Program Files\Internet Explorer\ folder
  folders.ExpandItem('|Desktop|My Computer|*C:*');
  folders.ExpandItem('|Desktop|My Computer|*C:*|Program Files');
  folders.ExpandItem('|Desktop|My Computer|*C:*|Program Files|Internet Explorer');

  aqUtils.Delay (500);

  // Collapse My Computer
  item := folders.wItems.Item['Desktop'].Items.Item['My Computer'];
  item.Collapse;
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:\Program Files\Internet Explorer\ folder
  folders["ExpandItem"]("|Desktop|My Computer|*C:*");
  folders["ExpandItem"]("|Desktop|My Computer|*C:*|Program Files");
  folders["ExpandItem"]("|Desktop|My Computer|*C:*|Program Files|Internet Explorer");

  aqUtils["Delay"] (500);

  // Collapse My Computer
  item = folders["wItems"]["Item"]("Desktop")["Items"]["Item"]("My Computer");
  item["Collapse"]();
}

You may note that the Win32TreeView object does not provide any methods that would let you expand or collapse all tree view items at once. However, you can create a script routine that does this. All the routine needs to do is iterate through the tree view items recursively and call the Expand (or Collapse) method of each tree view item. The following example illustrates how you can implement these routines:

Example

View description

JavaScript

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

  // Obtain the application process, window and the TreeView control
  p = Sys.Process("Project1");
  w = p.Window("TForm1", "Form1");
  TreeView = w.Window("TTreeView", "");

  // Expand all tree view items
  ExpandAllItems(TreeView);

  // Collapse all tree view items
  CollapseAllItems(TreeView);
}

function ExpandAllItems(TreeView)
{
  Log.Enabled = false;
  ExpandAll_Internal(TreeView.wItems);
  Log.Enabled = true;
  Log.Message("All tree view items were expanded.", TreeView.FullName);
}

function CollapseAllItems(TreeView)
{
  Log.Enabled = false;
  CollapseAll_Internal(TreeView.wItems);
  Log.Enabled = true;
  Log.Message("All tree view items were collapsed.", TreeView.FullName);
}

function ExpandAll_Internal(Items)
{
  let Item;

  for (let i=0; i<Items.Count; i++)
  {
    Item = Items.Item(i);
    // Expand the item
    Item.Expand();
    // Expand child items
    if (!strictEqual(Item.Items, null))
      ExpandAll_Internal(Item.Items);
  }
}

function CollapseAll_Internal(Items)
{
  let Item;

  for (let i=0; i<Items.Count; i++)
  {
    Item = Items.Item(i);
    // Collapse child items
    if (!strictEqual(Item.Items), null)
      CollapseAll_Internal(Item.Items);
    // Collapse the item
    Item.Collapse();
  }
}

JScript

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

  // Obtain the application process, window and the TreeView control
  p = Sys.Process("Project1");
  w = p.Window("TForm1", "Form1");
  TreeView = w.Window("TTreeView", "");

  // Expand all tree view items
  ExpandAllItems(TreeView);

  // Collapse all tree view items
  CollapseAllItems(TreeView);
}

function ExpandAllItems(TreeView)
{
  Log.Enabled = false;
  ExpandAll_Internal(TreeView.wItems);
  Log.Enabled = true;
  Log.Message("All tree view items were expanded.", TreeView.FullName);
}

function CollapseAllItems(TreeView)
{
  Log.Enabled = false;
  CollapseAll_Internal(TreeView.wItems);
  Log.Enabled = true;
  Log.Message("All tree view items were collapsed.", TreeView.FullName);
}

function ExpandAll_Internal(Items)
{
  var i, Item;

  for (i=0; i<Items.Count; i++)
  {
    Item = Items.Item(i);
    // Expand the item
    Item.Expand();
    // Expand child items
    if (Item.Items != null)
      ExpandAll_Internal(Item.Items);
  }
}

function CollapseAll_Internal(Items)
{
  var i, Item;

  for (i=0; i<Items.Count; i++)
  {
    Item = Items.Item(i);
    // Collapse child items
    if (Item.Items != null)
      CollapseAll_Internal(Item.Items);
    // Collapse the item
    Item.Collapse();
  }
}

Python

def Main():
  
  # Obtain the application process, window and the TreeView control
  p = Sys.Process("Project1")
  w = p.Window("TForm1", "Form1")
  TreeView = w.Window("TTreeView", "")

  # Expand all tree view items
  ExpandAllItems(TreeView)

  # Collapse all tree view items
  CollapseAllItems(TreeView)

def ExpandAllItems(TreeView):
  Log.Enabled = False
  ExpandAll_Internal(TreeView.wItems)
  Log.Enabled = True
  Log.Message("All tree view items were expanded.", TreeView.FullName)

def CollapseAllItems(TreeView):
  Log.Enabled = False
  CollapseAll_Internal(TreeView.wItems)
  Log.Enabled = True
  Log.Message("All tree view items were collapsed.", TreeView.FullName)

def ExpandAll_Internal(Items):

  for i in range(0, Items.Count):
    Item = Items.Item(i)
    # Expand the item
    Item.Expand()
    # Expand child items
    if (Item.Items != None):
      ExpandAll_Internal(Item.Items)

def CollapseAll_Internal(Items):

  for i in range(0, Items.Count):
    Item = Items.Item(i)
    # Collapse child items
    if (Item.Items != None):
      CollapseAll_Internal(Item.Items)
    # Collapse the item
    Item.Collapse()

VBScript

Sub Main
  Dim p, w, TreeView

  ' Obtain the application process, window and the TreeView control
  Set p = Sys.Process("Project1")
  Set w = p.Window("TForm1", "Form1")
  Set TreeView = w.Window("TTreeView", "")

  ' Expand all tree view items
  ExpandAllItems(TreeView)

  ' Collapse all tree view items
  CollapseAllItems(TreeView)
End Sub

Sub ExpandAllItems(TreeView)
  Log.Enabled = False
  ExpandAll_Internal(TreeView.wItems)
  Log.Enabled = True
  Call Log.Message("All tree view items were expanded.", TreeView.FullName)
End Sub

Sub ExpandAll_Internal(Items)
  Dim i, Item

  For i = 0 To Items.Count-1
    Set Item = Items.Item(i)
    ' Expand the item
    Item.Expand
    ' Expand child items
    If Not (Item.Items Is Nothing) Then
      ExpandAll_Internal(Item.Items)
    End If
  Next
End Sub

Sub CollapseAllItems(TreeView)
  Log.Enabled = False
  CollapseAll_Internal(TreeView.wItems)
  Log.Enabled = True
  Call Log.Message("All tree view items were collapsed.", TreeView.FullName)
End Sub

Sub CollapseAll_Internal(Items)
  Dim i, Item

  For i = 0 To Items.Count-1
    Set Item = Items.Item(i)
    ' Collapse child items
    If Not (Item.Items Is Nothing) Then
      CollapseAll_Internal(Item.Items)
    End If
    ' Collapse the item
    Item.Collapse
  Next
End Sub

DelphiScript

procedure ExpandAllItems(TreeView); forward;
procedure ExpandAll_Internal(Items); forward;
procedure CollapseAllItems(TreeView); forward;
procedure CollapseAll_Internal(Items); forward;

procedure Main;
var p, w, TreeView : OleVariant;
begin
  // Obtain the application process, window and the TreeView control
  p := Sys.Process('Project1');
  w := p.Window('TForm1', 'Form1');
  TreeView := w.Window('TTreeView', '');

  // Expand all tree view items
  ExpandAllItems(TreeView);

  // Collapse all tree view items
  CollapseAllItems(TreeView);
end;

procedure ExpandAllItems(TreeView);
begin
  Log.Enabled := false;
  ExpandAll_Internal(TreeView.wItems);
  Log.Enabled := true;
  Log.Message('All tree view items were expanded.', TreeView.FullName);
end;

procedure ExpandAll_Internal(Items);
var i, Item : OleVariant;
begin
  for i := 0 to Items.Count-1 do
  begin
    Item := Items.Item[i];
    // Expand the item
    Item.Expand;
    // Expand child items
    if Item.Items <> nil then
      ExpandAll_Internal(Item.Items);
  end;
end;

procedure CollapseAllItems(TreeView);
begin
  Log.Enabled := false;
  CollapseAll_Internal(TreeView.wItems);
  Log.Enabled := true;
  Log.Message('All tree view items were collapsed.', TreeView.FullName);
end;

procedure CollapseAll_Internal(Items);
var i, Item : OleVariant;
begin
  for i := 0 to Items.Count-1 do
  begin
    Item := Items.Item[i];
    // Collapse child items
    if Item.Items <> nil then
      CollapseAll_Internal(Item.Items);
    // Collapse the item
    Item.Collapse;
  end;
end;

C++Script, C#Script

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

  // Obtain the application process, window and the TreeView control
  p = Sys["Process"]("Project1");
  w = p["Window"]("TForm1", "Form1");
  TreeView = w["Window"]("TTreeView", "");

  // Expand all tree view items
  ExpandAllItems(TreeView);

  // Collapse all tree view items
  CollapseAllItems(TreeView);
}

function ExpandAllItems(TreeView)
{
  Log["Enabled"] = false;
  ExpandAll_Internal(TreeView["wItems"]);
  Log["Enabled"] = true;
  Log["Message"]("All tree view items were expanded.", TreeView["FullName"]);
}

function CollapseAllItems(TreeView)
{
  Log["Enabled"] = false;
  CollapseAll_Internal(TreeView["wItems"]);
  Log["Enabled"] = true;
  Log["Message"]("All tree view items were collapsed.", TreeView["FullName"]);
}

function ExpandAll_Internal(Items)
{
  var i, Item;

  for (i=0; i<Items["Count"]; i++)
  {
    Item = Items["Item"](i);
    // Expand the item
    Item["Expand"]();
    // Expand child items
    if (Item["Items"] != null)
      ExpandAll_Internal(Item["Items"]);
  }
}

function CollapseAll_Internal(Items)
{
  var i, Item;

  for (i=0; i<Items["Count"]; i++)
  {
    Item = Items["Item"](i);
    // Collapse child items
    if (Item["Items"] != null)
      CollapseAll_Internal(Item["Items"]);
    // Collapse the item
    Item["Collapse"]();
  }
}

Expanding Items with Mouse Clicks

It is possible to expand and collapse tree view items by clicking them when:

  • The tree view control has the auto-expand feature (that is, if it has the TVS_SINGLEEXPAND style), a single click on an item selects and automatically expands this item.
  • The tree view control does not support the auto-expand feature, a double-click on an item toggles between its expanded and collapsed states.

For example, the Folders tree in Windows Explorer can behave in both ways -- a user can turn the auto-expand feature on or off by using the Display simple folder view in Explorer’s Folders list option (to view or change the option value, select Tools | Options from Explorer’s main menu and switch to the View page).

In tests, you can simulate single-clicks on tree view items using the Win32TreeView.ClickItem or Win32TreeViewItem.Click actions. To simulate double-clicks, use the Win32TreeView.DblClickItem or Win32TreeViewItem.DblClick actions.

Below are two examples that illustrate how you can expand tree view items by clicking them. Both examples work with Windows Explorer’s Folders tree. The first example assumes that the Folders tree works in the auto-expand mode (that is, the Display simple folder view in Explorer's Folders list option is enabled), and the second example requires that the auto-expand mode be turned off.

Example 1

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:\Program Files\Internet Explorer\ folder
  folders.ClickItem("|Desktop|My Computer|*C:*");
  folders.ClickItem("Program Files");
  folders.ClickItem("Internet Explorer");
}

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:\Program Files\Internet Explorer\ folder
  folders.ClickItem("|Desktop|My Computer|*C:*")
  folders.ClickItem("Program Files")
  folders.ClickItem("Internet Explorer")

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 the C:\Program Files\Internet Explorer\ folder
  folders.ClickItem("|Desktop|My Computer|*C:*")
  folders.ClickItem("Program Files")
  folders.ClickItem("Internet Explorer")
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:\Program Files\Internet Explorer\ folder
  folders.ClickItem('|Desktop|My Computer|*C:*');
  folders.ClickItem('Program Files');
  folders.ClickItem('Internet Explorer');
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:\Program Files\Internet Explorer\ folder
  folders["ClickItem"]("|Desktop|My Computer|*C:*");
  folders["ClickItem"]("Program Files");
  folders["ClickItem"]("Internet Explorer");
}

Example 2

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:\Program Files\Internet Explorer\ folder
  folders.DblClickItem("|Desktop|My Computer|*C:*");
  folders.DblClickItem("Program Files");
  folders.DblClickItem("Internet Explorer");
}

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:\Program Files\Internet Explorer\ folder
  folders.DblClickItem("|Desktop|My Computer|*C:*")
  folders.DblClickItem("Program Files")
  folders.DblClickItem("Internet Explorer")

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 the C:\Program Files\Internet Explorer\ folder
  folders.DblClickItem("|Desktop|My Computer|*C:*")
  folders.DblClickItem("Program Files*")
  folders.DblClickItem("Internet Explorer")
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:\Program Files\Internet Explorer\ folder
  folders.DblClickItem('|Desktop|My Computer|*C:*');
  folders.DblClickItem('Program Files*');
  folders.DblClickItem('Internet Explorer');
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:\Program Files\Internet Explorer\ folder
  folders["DblClickItem"]("|Desktop|My Computer|*C:*");
  folders["DblClickItem"]("Program Files");
  folders["DblClickItem"]("Internet Explorer");
}

Simulating Keyboard Shortcuts

It is possible to expand items in tree view controls from the keyboard by using the Num Plus and Num Minus keys. Pressing the Num Plus key expands the currently selected item, and the Num Minus key press collapses it. The asterisk key on the numeric keyboard (Num *) allows you to expand the current tree view item recursively, that is, expand it along with all of its child items. You can send these keystrokes to the tree view control using the Keys action:

JavaScript, JScript

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

  // Obtain the application process, window and the TreeView control
  p = Sys.Process("Project1");
  w = p.Window("TForm1", "Form1");
  TreeView = w.Window("TTreeView", "");

  // Expand and collapse some items
  TreeView.SelectItem("|RootItem 2");
  TreeView.Keys("[NumPlus]");
  TreeView.SelectItem("|RootItem 4");
  TreeView.Keys("[NumAsterisk]");
  TreeView.SelectItem("|RootItem 4|SubItem 4-2");
  TreeView.Keys("[NumMinus]");
}

Python

def Main ():

  # Obtain the application process, window and the TreeView control
  p = Sys.Process("Project1")
  w = p.Window("TForm1", "Form1")
  TreeView = w.Window("TTreeView", "")

  # Expand and collapse some items
  TreeView.SelectItem("|RootItem 2")
  TreeView.Keys("[NumPlus]")
  TreeView.SelectItem("|RootItem 4")
  TreeView.Keys("[NumAsterisk]")
  TreeView.SelectItem("|RootItem 4|SubItem 4-2")
  TreeView.Keys("[NumMinus]")

VBScript

Sub Main
  Dim p, w, TreeView

  ' Obtain the application process, window and the TreeView control
  Set p = Sys.Process("Project1")
  Set w = p.Window("TForm1", "Form1")
  Set TreeView = w.Window("TTreeView", "")

  ' Expand and collapse some items
  TreeView.SelectItem("|RootItem 2")
  TreeView.Keys("[NumPlus]")
  TreeView.SelectItem("|RootItem 4")
  TreeView.Keys("[NumAsterisk]")
  TreeView.SelectItem("|RootItem 4|SubItem 4-2")
  TreeView.Keys("[NumMinus]")
End Sub

DelphiScript

procedure Main;
var p, w, TreeView : OleVariant;
begin
  // Obtain the application process, window and the TreeView control
  p := Sys.Process('Project1');
  w := p.Window('TForm1', 'Form1');
  TreeView := w.Window('TTreeView', '');

  // Expand and collapse some items
  TreeView.SelectItem('|RootItem 2');
  TreeView.Keys('[NumPlus]');
  TreeView.SelectItem('|RootItem 4');
  TreeView.Keys('[NumAsterisk]');
  TreeView.SelectItem('|RootItem 4|SubItem 4-2');
  TreeView.Keys('[NumMinus]');
end;

C++Script, C#Script

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

  // Obtain the application process, window and the TreeView control
  p = Sys["Process"]("Project1");
  w = p["Window"]("TForm1", "Form1");
  TreeView = w["Window"]("TTreeView", "");

  // Expand and collapse some items
  TreeView["SelectItem"]("|RootItem 2");
  TreeView["Keys"]("[NumPlus]");
  TreeView["SelectItem"]("|RootItem 4");
  TreeView["Keys"]("[NumAsterisk]");
  TreeView["SelectItem"]("|RootItem 4|SubItem 4-2");
  TreeView["Keys"]("[NumMinus]");
}

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
ExpandItem Action (TreeView Controls)
CollapseItem Action (TreeView Controls)
wExpanded Property (Specific to Win32TreeView Controls)
Expand Action (TreeViewItem Objects)
Collapse Action (TreeViewItem Objects)
Expanded Property (TreeViewItem Objects)

Highlight search results