Selecting a Single List Box Item

Applies to TestComplete 14.61, 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.

When working with a list box control, you often need to select an item. 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 that corresponds to this control. Use the following approaches to select the needed item:

  • You can select the needed item via the ClickItem, ClickItemXY, DblClickItem or DblClickItemXY actions. These type of actions perform clicks on the specified item of the list box control. Items can be specified by index or by caption, as provided in example below:

    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);

      // Select the specified item
      ListBox.ClickItem(6);
    }

    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)
    
      # Select the specified item
      ListBox.ClickItem(6)

    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)

      ' Select the specified item
      ListBox.ClickItem(6)
    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);

      // Select the specified item
      ListBox.ClickItem(6);
    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);

      // Select the specified item
      ListBox["ClickItem"](6);
    }

    Note: Item numeration within list box controls is zero-based, that is, the index of the first item is 0.
  • You can select the needed item via the SelectItem action. The needed item can be specified by its index or caption. Unlike the ClickItem action, SelectItem does not move the mouse pointer. Another difference is that SelectItem allows you to specify whether you want to select or deselect an item. Here is an example of selecting an item via this action:

    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);

      // Select the specified item
      ListBox.SelectItem(6, 1);
    }

    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)
    
      # Select the specified item
      ListBox.SelectItem(6, 1)

    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)

      ' Select the specified item
      ListBox.SelectItem 6, 1
    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);

      // Select the specified item
      ListBox.SelectItem(6, 1);
    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);

      // Select the specified item
      ListBox.SelectItem(6, 1);
    }

    If an item's captions change according to the current context, it may be useful to specify items only by constant parts of their names. To specify arbitrary parts in item names, TestComplete lets you use wildcard characters (* and ?) or regular expressions. The asterisk (*) corresponds to a string of any length (including an empty string), the question mark corresponds to any single character (including none). To specify more complicated parts of a caption, use regular expressions.

    If you wish to specify an asterisk (*) as part of an item’s caption, you should double it, because otherwise, it will be treated as a wildcard. Here is an example of how to select any item:

    ListBoxObj.ClickItem("*")

  • You can simulate keystrokes to navigate through the list box control. The list box control must be previously focused, then you should simulate arrow keys to select the needed item. For detailed information on how to simulate keystrokes, see Simulating Keystrokes. The following example demonstrates how to select an item:

    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);

      // Select the needed item
      ListBox.Keys("[Down][Down][Down][Down][Down]");
    }

    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)
    
      # Select the needed item
      ListBox.Keys("[Down][Down][Down][Down][Down]")

    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)

      ' Select the needed item
      ListBox.Keys "[Down][Down][Down][Down][Down]"
    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);

      // Select the needed item
      ListBox.Keys('[Down][Down][Down][Down][Down]');
    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);

      // Select the needed item
      ListBox["Keys"]("[Down][Down][Down][Down][Down]");
    }

  • If the tested control supports the incremental search functionality, you can search for an item by typing its text within the control. The control will search for the item while you are typing. The following code snippet selects an item whose caption begins with '2':

    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);

       // Select the item, which caption begins with "2"
       ListBox.Keys("2");
    }

    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)
    
      # Select the item, which caption begins with "2"
      ListBox.Keys("2")

    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)

      ' Select the item, which caption begins with "2"
       ListBox.Keys "2"
    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);

      // Select the item, which caption begins with "2"
      ListBox.Keys('2');
    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);
      
      // Select the item, which caption begins with "2"
      ListBox["Keys"]("2");
    }

See Also

Working With List Box Controls
ClickItem Action (ListBox Controls)
SelectItem Action (ListBox Controls)
Click Action
Simulating Keystrokes
About Object Browser

Highlight search results