Retrieving List Box Items in Desktop Windows Applications. Example

Applies to TestComplete 15.45, last modified on December 01, 2022

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

The following script routine demonstrates how you can work with TListBox items (from the Borland VCL) directly from a TestComplete script using methods and properties of the Win32ListBox object. This example assumes that the application displays a form with a TListBox control on it. First, SaveItems retrieves the selected list box items and then posts them to the test log.

JavaScript, JScript

function LogSelectedItems()
{
  var p, lb, i;

  // Obtains the list box object
  p = Sys.Process("Project1");
  lb = p.Window("TForm1", "Form1").Window("TListBox", "ListBox1");

  // Goes through the list box items
  for (i=0; i<lb.wItemCount; i++)
    // If an item is selected, ...
    if (lb.wSelected(i))
      // ... posts the item caption to the log
      Log.Message(lb.wItem(i));
}

Python

def LogSelectedItems():

  # Obtains the list box object
  p = Sys.Process("Project1")
  lb = p.Window("TForm1", "Form1").Window("TListBox", "ListBox1")

  # Goes through the list box items 
  for i in (0, lb.wItemCount-1):
    # If an item is selected, ...
    if lb.wSelected[i]:
      # ... posts the item caption to the log
      Log.Message(lb.wItem[i])

VBScript

Sub LogSelectedItems
  Dim p, lb, i

  ' Obtains the list box object
  Set p = Sys.Process("Project1")
  Set lb = p.Window("TForm1", "Form1").Window("TListBox", "ListBox1")

  ' Goes through the list box items
  For i = 0 To lb.wItemCount - 1
    ' If an item is selected, ...
    If lb.wSelected(i) Then
      ' ... posts the item caption to the log
      Log.Message(lb.wItem(i))
    End If
  Next
End Sub

DelphiScript

procedure LogSelectedItems;
var p, lb, i : OleVariant;
begin
  // Obtains the list box object
  p := Sys.Process('Project1');
  lb := p.Window('TForm1', 'Form1').Window('TListBox', 'ListBox1');

  // Goes through the list box items
  for i := 0 to lb.wItemCount-1 do
    // If an item is selected, ...
    if lb.wSelected[i] then
      // ... posts the item caption to the log
      Log.Message(lb.wItem[i]);
end;

C++Script, C#Script

function LogSelectedItems()
{
  var p, lb, i;

  // Obtains the list box object
  p = Sys["Process"]("Project1");
  lb = p["Window"]("TForm1", "Form1")["Window"]("TListBox", "ListBox1");

  // Goes through the list box items
  for (i=0; i<lb["wItemCount"]; i++)
    // If an item is selected, ...
    if (lb["wSelected"](i))
      // ... posts the item caption to the log
      Log["Message"](lb["wItem"](i));
}

See Also

Working With List Box Controls in Desktop Windows Applications
Working With List Box Controls in Web Applications

Highlight search results