Changing List View Items' Captions in Desktop Windows Applications

Applies to TestComplete 15.40, last modified on March 25, 2022

List view controls can be configured so that they allow the user to edit the captions of its items. The in-place editor for the selected list view item is activated when a user clicks this item. The list 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 and forces the list view control to update the item’s caption.

While testing list 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.

To obtain captions of list view items and subitems, you can use the Win32ListView.wItem 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 script that will perform the following actions:

  • Selects the desired list view item, and then clicks it. You can do this using the Win32ListView.SelectItem and Win32ListView.ClickItem actions, respectively.

  • Obtains a Win32Edit object that provides scripting access to the in-place edit box. It is a child object of the list view control and can be addressed in the following form:

    listViewObj.Window("Edit", ItemCaption)

    In this statement, listViewObj is a Win32ListView object that corresponds to the tested list view control, “Edit” is the edit box’s class name and ItemCaption is the caption of the item being edited.

  • Enters new text in the edit box or sets it using either the SetText action or the wText property, and then presses the Enter key to finish editing. To simulate key presses, you can use the Keys action applied to the edit box control.

Note: List view controls allow users to only modify the items’ text directly; editing of subitems can only be implemented in the application by using special dialogs.

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

  • AListView - A Win32ListView object.
  • AItem - The caption or zero-based index of the item whose caption you want to change.
  • NewText - A string that holds the item’s new caption.

JavaScript, JScript

function Main()
{
  var p, ListView;

  p = Sys.Process("ListViewSample");
  ListView = p.Window("MyWndClass", "Form1").Window("SysListView32");

  SetItemText(ListView, "Item 1", "The first item");
  SetItemText(ListView, "Item 3", "The third item");
  SetItemText(ListView, "Item 5", "The fifth item");
}

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

  // Obtain the edit box object
  var Edit = AListView.Window("Edit", "*");

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

Python

def Main():

  p = Sys.Process("ListViewSample")
  ListView = p.Window("MyWndClass", "Form1").Window("SysListView32")

  SetItemText(ListView, "Item 1", "The first item")
  SetItemText(ListView, "Item 3", "The third item")
  SetItemText(ListView, "Item 5", "The fifth item")

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

  # Obtain the edit box object
  Edit = AListView.Window("Edit", "*")

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

VBScript

Sub Main
  Dim p, ListView

  Set p = Sys.Process("ListViewSample")
  Set ListView = p.Window("MyWndClass", "Form1").Window("SysListView32")

  Call SetItemText(ListView, "Item 1", "The first item")
  Call SetItemText(ListView, "Item 3", "The third item")
  Call SetItemText(ListView, "Item 5", "The fifth item")
End Sub

Sub SetItemText (AListView, AItem, NewText)
  Dim Edit

  ' Invoke the in-place editor for the specified item
  AListView.SelectItem(AItem)
  AListView.ClickItem(AItem)

  ' Obtain the edit box object
  Set Edit = AListView.Window("Edit", "*")

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

DelphiScript

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

  // Obtain the edit box object
  Edit := AListView.Window('Edit', '*');

  // Enter new text
  Edit.wText := NewText;
  Edit.Keys('[Enter]');
  // or --
  // Edit.Keys('^a[Del]' + NewText + '[Enter]');
end;

procedure Main;
var p, ListView : OleVariant;
begin
  p := Sys.Process('ListViewSample');
  ListView := p.Window('MyWndClass', 'Form1').Window('SysListView32');

  SetItemText(ListView, 'Item 1', 'The first item');
  SetItemText(ListView, 'Item 3', 'The third item');
  SetItemText(ListView, 'Item 5', 'The fifth item');
end;

C++Script, C#Script

function Main()
{
  var p, ListView;

  p = Sys["Process"]("ListViewSample");
  ListView = p["Window"]("MyWndClass", "Form1")["Window"]("SysListView32");

  SetItemText(ListView, "Item 1", "The first item");
  SetItemText(ListView, "Item 3", "The third item");
  SetItemText(ListView, "Item 5", "The fifth item");
}

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

  // Obtain the edit box object
  var Edit = AListView["Window"]("Edit", "*");

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

See Also

Working With List View Controls in Desktop Windows Applications
wItem Property (ListView Controls)

Highlight search results