Changing Tree View Items' Captions in Desktop Windows Applications

Applies to TestComplete 15.63, last modified on April 23, 2024

Tree view controls can be configured so that they allow the user editing the captions of its items. The in-place editor for the selected tree view item is activated when a user clicks this item. At that, the tree view creates and displays an edit box holding the item text. In this box, the user can enter the new item text. The Enter key press finishes the editing. After that, the tree view control updates the item’s caption.

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.

You can obtain a tree view item’s caption using the Win32TreeViewItem.Text property. However, this property is read-only and does not let you modify its data. In order to change the item’s caption, you need to create a test that will perform the following actions:

  • Selects the desired tree view item, and then clicks it. For this purpose, you can use the SelectItem and ClickItem actions of a Win32TreeView object that corresponds to the tested tree view control, or the Select and Click actions of a Win32TreeViewItem object that corresponds to the desired tree view item.
  • Obtains a Win32Edit object that provides scripting access to the in-place edit box. You can do this in two ways:
    1. In most cases, the in-place edit box is a child object of the tree view control, so it can be addressed in the following form:
      treeViewObj.Window("Edit", ItemCaption)
      Here treeViewObj is a Win32TreeView object that corresponds to the tested tree view control, “Edit” is the edit box’s class name and ItemCaption is the caption of the item being edited.
    2. Since the edit box is called for the item that has been clicked, it is situated under the mouse pointer. So, you can obtain the edit control using the Sys.Desktop.ObjectFromPoint method.
  • Enters the new text in the edit box and presses the Enter key to finish editing. To simulate key presses, use the Keys action applied to the edit box control.

    You can speed up the script execution by assigning the new text to the edit box’s wText rather that “typing” it. Note that the Enter key press is still required in order to finish editing.

The following example illustrates the described approach. It contains the SetItemText routine that changes the text of a tree view item. The routine has tree parameters:

Note: The SetItemText routine implements several approaches for obtaining the in-place editor object and changing its text (alternative code is commented out). You can experiment with various combinations of scripting statements to detect the code that works best for your tested application.

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

  // Change the captions of some items
  SetItemText(TreeView, "|Item 4|Item 4-1", "New Text");
  SetItemText(TreeView, "|Item 5", "LastItem");
}

function SetItemText (TreeView, ItemPath, NewText)
{
  // Invoke the in-place editor for the specified item
  TreeView.SelectItem(ItemPath);
  TreeView.ClickItem(ItemPath);

  // Obtain the edit box object
  Edit = TreeView.Window("Edit", "*");
  // or --
  // aqUtils.Delay (500); // A small delay may be needed
  // Edit = Sys.Desktop.ObjectFromPoint(Sys.Desktop.MouseX, Sys.Desktop.MouseY);

  // Enter new text
  Edit.wText = NewText;
  Edit.Keys("[Enter]");
  // or --
  // Edit.Keys(NewText + "[Enter]");
}

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

  # Change the captions of some items
  SetItemText(TreeView, "|Item 4|Item 4-1", "New Text")
  SetItemText(TreeView, "|Item 5", "LastItem")

def SetItemText (TreeView, ItemPath, NewText):
  
  # Invoke the in-place editor for the specified item
  TreeView.SelectItem(ItemPath)
  TreeView.ClickItem(ItemPath)

  # Obtain the edit box object
  Edit = TreeView.Window("Edit", "*")
  # or --
  # aqUtils.Delay (500) # A small delay may be needed
  # Edit = Sys.Desktop.ObjectFromPoint(Sys.Desktop.MouseX, Sys.Desktop.MouseY)

  # Enter new text
  Edit.wText = NewText
  Edit.Keys("[Enter]")
  # or --
  # Edit.Keys(NewText + "[Enter]")

VBScript

Sub Main_ChangeCaption
  Dim p, w, TreeView, Item

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

  ' Change the captions of some items
  Call SetItemText (TreeView, "|Item 4|Item 4-1", "New Text")
  Call SetItemText (TreeView, "|Item 5", "Last Item")
End Sub

Sub SetItemText (TreeView, ItemPath, NewText)
  Dim Edit

  ' Invoke the in-place editor for the specified item
  TreeView.SelectItem(ItemPath)
  TreeView.ClickItem(ItemPath)

  ' Obtain the edit box object
  Set Edit = TreeView.Window("Edit", "*")
  ' or --
  ' aqUtils.Delay (500) ' A small delay may be needed
  ' Set Edit = Sys.Desktop.ObjectFromPoint(Sys.Desktop.MouseX, Sys.Desktop.MouseY)

  ' Enter new text
  Edit.wText = NewText
  Edit.Keys("[Enter]")
  ' or --
  ' Edit.Keys(NewText & "[Enter]")
End Sub

DelphiScript

procedure SetItemText (TreeView, ItemPath, NewText); 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', '');

  // Change the captions of some items
  SetItemText(TreeView, '|Item 4|Item 4-1', 'New Text');
  SetItemText(TreeView, '|Item 5', 'Last item');
end;

procedure SetItemText (TreeView, ItemPath, NewText);
var Edit : OleVariant;
begin
  // Invoke the in-place editor for the specified item
  TreeView.SelectItem(ItemPath);
  TreeView.ClickItem(ItemPath);

  // Obtain the edit box object
  Edit := TreeView.Window('Edit', '*');
  // or --
  // aqUtils.Delay (500); // A small delay may be needed
  // Edit := Sys.Desktop.ObjectFromPoint(Sys.Desktop.MouseX, Sys.Desktop.MouseY);

  // Enter new text
  Edit.wText := NewText;
  Edit.Keys('[Enter]');
  // or --
  // Edit.Keys(NewText + '[Enter]');
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", "");

  // Change the captions of some items
  SetItemText(TreeView, "|Item 4|Item 4-1", "New Text");
  SetItemText(TreeView, "|Item 5", "LastItem");
}

function SetItemText (TreeView, ItemPath, NewText)
{
  // Invoke the in-place editor for the specified item
  TreeView["SelectItem"](ItemPath);
  TreeView["ClickItem"](ItemPath);

  // Obtain the edit box object
  Edit = TreeView["Window"]("Edit", "*");
  // or --
  // aqUtils["Delay"] (500); // A small delay may be needed
  // Edit = Sys["Desktop"]["ObjectFromPoint"](Sys["Desktop"]["MouseX"], Sys["Desktop"]["MouseY"]);

  // Enter new text
  Edit["wText"] = NewText;
  Edit["Keys"]("[Enter]");
  // or --
  // Edit["Keys"](NewText + "[Enter]");
}

See Also

Working With Tree View Controls in Desktop Windows Applications
Text Property (TreeViewItem Objects)
Win32 Edit Support

Highlight search results