Getting Several List Box Items 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 its items to process them. If the tested control is a standard Windows list box control, you can use specific properties and methods provided by the Win32ListBox object, which corresponds to this control. You can get several items one by one via the wItem property of the Win32ListBox object (for detailed information on how to obtain a single item see Getting a Single List Box Item in Desktop Windows Applications), but if the quantity of the needed items is rather large, this approach is inconvenient.

The Win32ListBox object provides specific properties, which allow you to obtain a list of the needed items. You can either use the wSelectedItems property or the wItemList property. wSelectedItems returns a list of currently selected items, while wItemList returns all of the items that the list box contains. Both of these properties return a string, which contains the needed items, separated by the delimiter.

You can get or set the separating symbols via the wListSeparator property. The delimiter can contain several symbols as well as one symbol. After determining the delimiter you can extract the items from the obtained string as provided in the example below:

JavaScript, JScript

function Main()
{
  var p, w, i, ListBox, Item, Del, ItemString;

  // 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 the list of items and current delimiter
  ItemString = ListBox.wItemList
  Del = ListBox.wListSeparator
  aqString.ListSeparator = Del;

  // Extract particular items and post them to log
  for (i = 0; i < ListBox.wItemCount; i++)
  {
    Item = aqString.GetListItem(ItemString, i);
    Log.Message(Item);
  }
}

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 the list of items and current delimiter
  ItemString = ListBox.wItemList
  Del = ListBox.wListSeparator
  aqString.ListSeparator = Del

  # Extract particular items and post them to log 
  for i in (0, ListBox.wItemCount-1):
    Item = aqString.GetListItem(ItemString, i)
    Log.Message(Item)

VBScript

Sub Main
  Dim p, w, i, ListBox, Item, Del, ItemString

  ' 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 the list of items and current delimiter
  ItemString = ListBox.wItemList
  Del = ListBox.wListSeparator
  aqString.ListSeparator = Del

  ' Extract particular items and post them to log
  For i = 0 To ListBox.wItemCount - 1
    Item = aqString.GetListItem(ItemString, i)
    Log.Message(Item)
  Next
End Sub

DelphiScript

procedure Main;
var p, w, i, ListBox, Item, Del, ItemString : 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 the list of items and current delimiter
  ItemString := ListBox.wItemList;
  Del := ListBox.wListSeparator;
  aqString.ListSeparator := Del;

  // Extract particular items and post them to log
  for i := 0 to ListBox.wItemCount-1 do
  begin
    Item := aqString.GetListItem(ItemString, i);
    Log.Message(Item);
  end
end;

C++Script, C#Script

function Main()
{
  var p, w, i, ListBox, Item, Del, ItemString;

  // 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 the list of items and current delimiter
  ItemString = ListBox["wItemList"];
  Del = ListBox["wListSeparator"];
  aqString["ListSeparator"] = Del;

  // Extract particular items and post them to log
  for (i=0; i<ListBox["wItemCount"]; i++)
  {
    Item = aqString["GetListItem"](ItemString, i);
    Log["Message"](Item);
  }
}

See Also

Working With List Box Controls in Desktop Windows Applications
wSelectedItems Property
wItemList Property
Getting a Single List Box Item in Desktop Windows Applications
Working With List Box Controls in Web Applications

Highlight search results