Checking Whether a List Box Item Is Selected

Applies to TestComplete 14.71, last modified on April 22, 2021

While testing list box 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 check whether the list box item that you want to process is selected. For example, you may need to check the item’s state before performing certain actions that rely on the state.

You can determine the item’s state by using specific properties and methods of the Win32ListBox object. This object provides access to standard Windows list box controls. The wSelected property allows you to check whether the needed item is selected. You can specify the item by its caption or index. If the specified item is selected, the wSelected property returns True, otherwise False.

The following code example copies the item caption to the clipboard if it is selected, otherwise it posts a message to the log:

JavaScript, JScript

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

  // Obtain list box object
  p = Sys.Process("wordpad");
  Sys.Keys("~i[Enter]");
  w = p.Window("#32770", "Date and Time");
  ListBox = w.Window("ListBox", "", 1);

   // Check whether the specified item is selected
  if (ListBox.wSelected(3))
    Sys.Clipboard = ListBox.wItem(3)
  else
    Log.Message("Item is not selected");
}

Python

def Main():
  
  # Obtain list box object 
  p = Sys.Process("wordpad")
  Sys.Keys("~i[Enter]")
  w = p.Window("#32770", "Date and Time")
  ListBox = w.Window("ListBox", "", 1) 

  # Check whether the specified item is selected 
  if ListBox.wSelected[3]: 
    Sys.Clipboard = ListBox.wItem[3]
  else:
    Log.Message("Item is not selected")

VBScript

Sub Main
Dim p, w, ListBox

  ' Obtain list box object
  Set p = Sys.Process("wordpad")
  Sys.Keys "~i[Enter]"
  Set w = p.Window("#32770", "Date and Time")
  Set ListBox = w.Window("ListBox", "", 1)

  ' Check whether the specified item is selected
  If ListBox.wSelected(3) Then
    Sys.Clipboard = ListBox.wItem(3)
  Else
    Log.Message("Item is not selected")
  End If
End Sub

DelphiScript

procedure Main;
var p, w, ListBox : OleVariant;
begin

  // Obtain list box object
  p := Sys.Process('wordpad');
  Sys.Keys('~i[Enter]');
  w := p.Window('#32770', 'Date and Time', 1);
  ListBox := w.Window('ListBox', '', 1);

  // Check whether the specified item is selected
  if ListBox.wSelected[3] then
    Sys.Clipboard := ListBox.wItem[3]
  else
    Log.Message('Item is not selected');
end;

C++Script, C#Script

function Main()
{
var p, w, ListBox;
 
  // Obtain list box object
  p = Sys["Process"]("wordpad");
  Sys.Keys("~i[Enter]");
  w = p["Window"]("#32770", "Date and Time", 1);
  ListBox = w.Window("ListBox", "", 1);
    
  // Check whether the specified item is selected
   if (ListBox["wSelected"](3))
    Sys["Clipboard"] = ListBox["wItem"](3)
  else
    Log["Message"]("Item is not selected");
    
}

See Also

Working With List Box Controls
wSelected Property (ListBox Controls)

Highlight search results