Checking and Unchecking List View Items in Desktop Windows Applications

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

A list view control can display check boxes (or custom state images) next to items. Check boxes enable multiple selection capabilities in the list view, in addition to ordinary multiple selection via Ctrl- and Shift-clicks. If the list view control does not support multiple selection, check boxes provide this capability allowing the users to check the items they want to work with.

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 tests, you may need to simulate checking and unchecking of list view items. The Win32ListView object provides the special CheckItem action that lets you do this. The action has two parameters that specify the target list view item and its desired state. For more information on them, see the action description. A sample script below demonstrates how you can use the CheckItem action:

JavaScript, JScript

function Test()
{
  var p, ListView;

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

  ListView.CheckItem("Item 1", true);
  ListView.CheckItem("Item 4", true);
  ListView.CheckItem("Item 7", false);
}

Python

def Test():

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

  ListView.CheckItem("Item 1", True)
  ListView.CheckItem("Item 4", True)
  ListView.CheckItem("Item 7", False)

VBScript

Sub Test
  Dim p, ListView

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

  Call ListView.CheckItem("Item 1", True)
  Call ListView.CheckItem("Item 4", True)
  Call ListView.CheckItem("Item 7", False)
End Sub

DelphiScript

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

  ListView.CheckItem('Item 1', true);
  ListView.CheckItem('Item 4', true);
  ListView.CheckItem('Item 7', false);
end;

C++Script, C#Script

function Test()
{
  var p, ListView;

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

  ListView["CheckItem"]("Item 1", true);
  ListView["CheckItem"]("Item 4", true);
  ListView["CheckItem"]("Item 7", false);
}

You can also toggle the item’s state between checked and unchecked by selecting it and pressing the Space key. In certain list view controls, a double-click on an item toggles its state as well. To simulate keypresses, use the Keys action applied to the list view control; to simulate double-clicks on list view items -- the Win32ListView.DblClickItem action. The following code snippet demonstrates how you can use these actions in scripts:

JavaScript, JScript

function Test()
{
  var p, ListView;

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

  ListView.SelectItem("Item 1");
  ListView.Keys(" ");

  ListView.DblClickItem("Item 4");
  ListView.DblClickItem("Item 7");
}

Python

def Test():

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

  ListView.SelectItem("Item 1")
  ListView.Keys(" ")

  ListView.DblClickItem("Item 4")
  ListView.DblClickItem("Item 7")

VBScript

Sub Test
  Dim p, ListView

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

  ListView.SelectItem("Item 1")
  ListView.Keys(" ")

  ListView.DblClickItem("Item 4")
  ListView.DblClickItem("Item 7")
End Sub

DelphiScript

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

  ListView.SelectItem('Item 1');
  ListView.Keys(' ');

  ListView.DblClickItem('Item 4');
  ListView.DblClickItem('Item 7');
end;

C++Script, C#Script

function Test()
{
  var p, ListView;

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

  ListView["SelectItem"]("Item 1");
  ListView["Keys"](" ");

  ListView["DblClickItem"]("Item 4");
  ListView["DblClickItem"]("Item 7");
}

To determine whether a list view item is checked, use the wChecked property. It returns True is the specified item is checked and False otherwise. You can find a sample script that checks the wChecked property value in the Getting the Item’s Checked State section of the Checking List View Items' State in Desktop Windows Applications topic.

See Also

Working With List View Controls in Desktop Windows Applications
CheckItem Action (ListView Controls)
wChecked Property (ListView Controls)

Highlight search results