Getting the Selected List View Item(s) in Desktop Windows Applications

Applies to TestComplete 15.70, last modified on December 17, 2024

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.

In your tests, you may need to know which list view item is currently focused, or to determine the range of the selected items. For both purposes, you can use the wSelectedItems property of the Win32ListView object that corresponds to the tested list view control.

If one item is currently selected in the list view control, the wSelectedItems property returns the caption of this item:

JavaScript, JScript

function Test ()
{
  var p, ListView;

  // Obtain the process object and the ListView control
  p = Sys.Process("ListViewSample");
  ListView = p.Window("MyWndClass", "Form1").Window("SysListView32");

  // Select an item
  ListView.ClickItem(3);
  Log.Message("Selected item: " + ListView.wSelectedItems);
}

Python

def Test():

  # Obtain the process object and the ListView control
  p = Sys.Process("ListViewSample")
  ListView = p.Window("MyWndClass", "Form1").Window("SysListView32")

  # Select an item
  ListView.ClickItem(3)
  Log.Message("Selected item: " + ListView.wSelectedItems)

VBScript

Sub Test
  Dim p, ListView

  ' Obtain the process object and the ListView control
  Set p = Sys.Process("ListViewSample")
  Set ListView = p.Window("MyWndClass", "Form1").Window("SysListView32")

  ' Select an item
  ListView.ClickItem(3)
  Log.Message("Selected item: " & ListView.wSelectedItems)
End Sub

DelphiScript

procedure Test;
var p, ListView;
begin
  // Obtain the process object and the ListView control
  p := Sys.Process('ListViewSample');
  ListView := p.Window('MyWndClass', 'Form1').Window('SysListView32');

  // Select an item
  ListView.ClickItem(3);
  Log.Message('Selected item: ' + ListView.wSelectedItems);
end;

C++Script, C#Script

function Test ()
{
  var p, ListView;

  // Obtain the process object and the ListView control
  p = Sys["Process"]("ListViewSample");
  ListView = p["Window"]("MyWndClass", "Form1")["Window"]("SysListView32");

  // Select an item
  ListView["ClickItem"](3);
  Log["Message"]("Selected item: " + ListView["wSelectedItems"]);
}

If the list view control works in the multi-select mode and several items are currently selected in it, the wSelectedItems property returns the list of selected items’ captions separated by the wListSeparator characters. You can then parse the returned string to obtain captions of individual items and determine the items count:

JavaScript, JScript

function Test ()
{
  var p, ListView, n;

  // Obtain the process object and the ListView control
  p = Sys.Process("ListViewSample");
  ListView = p.Window("MyWndClass", "Form1").Window("SysListView32");

  // Select a range of items
  ListView.ClickItem(1);
  ListView.ClickItem(4, 0, skShift);

  // Post information about the selected items to the log
  ListView.wListSeparator = ", ";
  aqString.ListSeparator = ListView.wListSeparator;
  n = aqString.GetListLength(ListView.wSelectedItems);
  Log.Message("Selected items: " + ListView.wSelectedItems);
  Log.Message("Number of selected items: " + n);
}

Python

def Test():

  # Obtain the process object and the ListView control
  p = Sys.Process("ListViewSample")
  ListView = p.Window("MyWndClass", "Form1").Window("SysListView32")

  # Select a range of items
  ListView.ClickItem(1)
  ListView.ClickItem(4, 0, skShift)

  # Post information about the selected items to the log
  ListView.wListSeparator = ", "
  aqString.ListSeparator = ListView.wListSeparator
  n = aqString.GetListLength(ListView.wSelectedItems)
  Log.Message("Selected items: " + ListView.wSelectedItems)
  Log.Message("Number of selected items: " + n)

VBScript

Sub Test
  Dim p, ListView, n

  ' Obtain the process object and the ListView control
  Set p = Sys.Process("ListViewSample")
  Set ListView = p.Window("MyWndClass", "Form1").Window("SysListView32")

  ' Select a range of items
  ListView.ClickItem(1)
  Call ListView.ClickItem(4, 0, skShift)

  ' Post information about the selected items to the log
  ListView.wListSeparator = ", "
  aqString.ListSeparator = ListView.wListSeparator
  n = aqString.GetListLength(ListView.wSelectedItems)
  Log.Message("Selected items: " & ListView.wSelectedItems)
  Log.Message("Number of selected items: " & n)
End Sub

DelphiScript

procedure Test;
var p, ListView, n;
begin
  // Obtain the process object and the ListView control
  p := Sys.Process('ListViewSample');
  ListView := p.Window('MyWndClass', 'Form1').Window('SysListView32');

  // Select a range of items
  ListView.ClickItem(1);
  ListView.ClickItem(4, 0, skShift);

  // Post information about the selected items to the log
  ListView.wListSeparator := ', ';
  aqString.ListSeparator := ListView.wListSeparator;
  n := aqString.GetListLength(ListView.wSelectedItems);
  Log.Message('Selected items: ' + ListView.wSelectedItems);
  Log.Message('Number of selected items: ' + aqConvert.VarToStr(n));
end;

C++Script, C#Script

function Test ()
{
  var p, ListView, n;

  // Obtain the process object and the ListView control
  p = Sys["Process"]("ListViewSample");
  ListView = p["Window"]("MyWndClass", "Form1")["Window"]("SysListView32");

  // Select a range of items
  ListView["ClickItem"](1);
  ListView["ClickItem"](4, 0, skShift);

  // Post information about the selected items to the log
  ListView["wListSeparator"] = ", ";
  aqString["ListSeparator"] = ListView["wListSeparator"];
  n = aqString["GetListLength"](ListView["wSelectedItems"]);
  Log["Message"]("Selected items: " + ListView["wSelectedItems"]);
  Log["Message"]("Number of selected items: " + n);
}

See Also

Working With List View Controls in Desktop Windows Applications

Highlight search results