Getting a Single List Box Item in Desktop Windows Applications

Applies to TestComplete 15.47, last modified on January 20, 2023

Note: To learn how to simulate user actions over list box controls in web applications, see Working With List Box Controls in Web Applications.

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.

When working with a list box control, you may need to obtain an item to process it. If the control that you are testing is a standard Windows list box control, you can use specific properties and methods of the Win32ListBox object, which corresponds to this control.

You can use the wItem property to obtain the needed item specified by its index. The following code snippet obtains the specified item, copies its text to the clipboard and then posts the clipboard's contents to the test 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);

  // Obtain item caption and post it to the log
  Sys.Clipboard = ListBox.wItem(2);
  Log.Message(Sys.Clipboard);
}

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)
  
  # Obtain item caption and post it to the log 
  Sys.Clipboard = ListBox.wItem[2]
  Log.Message(Sys.Clipboard)

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)

  ' Obtain item caption and post it to the log
   Sys.Clipboard = ListBox.wItem(2)
   Log.Message(Sys.Clipboard)
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);

  // Obtain item caption and post it to the log
  Sys.Clipboard := ListBox.wItem[2];
  Log.Message(Sys.Clipboard);
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);

  // Obtain item caption and post it to the log
  Sys["Clipboard"] = ListBox["wItem"](2);
  Log["Message"](Sys["Clipboard"]);
}

Note: Numeration of the list box’s items is zero-based, that is the index of the first item is 0.

See Also

Working With List Box Controls in Desktop Windows Applications
wItem Property (ListBox Controls)
Working With List Box Controls in Web Applications

Highlight search results