Selecting Combo Box Items in Desktop Windows Applications

Applies to TestComplete 15.68, last modified on October 16, 2024

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

While testing combo 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 combo box control, you select items. Combo box controls do not permit multiple selection. There are several types of combo boxes, but in general, the principles of working with them are similar for all types. If the tested application uses standard Windows combo boxes, you can use specific properties and methods, provided by the Win32ComboBox object.

  • The ClickItem action allows you to select any item specified by its index or caption. The following example selects an item with the caption Western, then selects the fourth item in the combo box:

    JavaScript, JScript

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

      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);

      // Obtain a combo box object
      p = Sys.Process("NOTEPAD");
      Sys.Keys("~of");
      w = Sys.Process("NOTEPAD").Window("#32770", "Font", 1);
      ComboBox = w.Window("ComboBox", "", 5);
      
      // Select items
      ComboBox.ClickItem("Western");
      ComboBox.ClickItem(4);
    }

    Python

    def Main():
      
      # Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL)
    
      # Obtain a combo box object 
      p = Sys.Process("NOTEPAD")
      Sys.Keys("~of")
      w = Sys.Process("NOTEPAD").Window("#32770", "Font", 1)
      ComboBox = w.Window("ComboBox", "", 5)
      
      # Select items
      ComboBox.ClickItem("Western")
      ComboBox.ClickItem(4)

    VBScript

    Sub Main
    Dim p, w, ComboBox

      ' Run Notepad
      Call WshShell.Run("notepad.exe", SW_SHOWNORMAL)
      
      ' Obtain a combo box object
      Set p = Sys.Process("NOTEPAD")
      Sys.Keys "~of"
      Set w = Sys.Process("NOTEPAD").Window("#32770", "Font", 1)
      Set ComboBox = w.Window("ComboBox", "", 5)
      
      ' Select items
      ComboBox.ClickItem "Western"
      ComboBox.ClickItem (4)
    End Sub

    DelphiScript

    procedure Main();
    var
       p, w, ComboBox: OleVariant;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);
      
      // Obtain a combo box object
      p := Sys.Process('NOTEPAD');
      Sys.Keys('~of');
      w := Sys.Process('NOTEPAD').Window('#32770', 'Font', 1);
      ComboBox := w.Window('ComboBox', '', 5);
      
      // Select items
      ComboBox.ClickItem('Western');
      ComboBox.ClickItem(4);
    end;

    C++Script, C#Script

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

      // Run Notepad
      WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);
      
      // Obtain a combo box object
      p = Sys["Process"]("NOTEPAD");
      Sys.Keys("~of");
      w = Sys["Process"]("NOTEPAD").Window("#32770", "Font", 1);
      ComboBox = w["Window"]("ComboBox", "", 5);
      
      // Select items
      ComboBox["ClickItem"]("Western");
      ComboBox["ClickItem"](4);
    }

    If captions of combo box items 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:

    ComboBoxObj.ClickItem("*")

  • You can simulate typing an item's caption (for detailed information on how to do this, see Simulating Keystrokes). If you are working with a simple or drop-down combo box, the typed text will be displayed within the edit box, and if this text corresponds to any of the combo box's items, this item will be selected. Drop-down list boxes do not allow you to enter text, but they support incremental search. Thus, the control will search for the item while you are typing. The following example demonstrates how to select an item by typing its text:

    JavaScript, JScript

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

      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);

      // Obtain a combo box object
      p = Sys.Process("NOTEPAD");
      Sys.Keys("~of");
      w = Sys.Process("NOTEPAD").Window("#32770", "Font", 1);
      ComboBox = w.Window("ComboBox", "", 3);
      
      // Select the item
      ComboBox.Keys("14");
    }

    Python

    def Main():
      
      # Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL)
    
      # Obtain a combo box object 
      p = Sys.Process("NOTEPAD")
      Sys.Keys("~of")
      w = Sys.Process("NOTEPAD").Window("#32770", "Font", 1)
      ComboBox = w.Window("ComboBox", "", 3)
      
      # Select the item
      ComboBox.Keys("14")

    VBScript

    Sub Main
    Dim p, w, ComboBox

      ' Run Notepad
      Call WshShell.Run("notepad.exe", SW_SHOWNORMAL)
      
      ' Obtain a combo box object
      Set p = Sys.Process("NOTEPAD")
      Sys.Keys "~of"
      Set w = Sys.Process("NOTEPAD").Window("#32770", "Font", 1)
      Set ComboBox = w.Window("ComboBox", "", 3)
      
      'Select the item
      ComboBox.Keys "14"
    End Sub

    DelphiScript

    procedure Main();
    var
       p, w, ComboBox: OleVariant;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);
      
      // Obtain a combo box object
      p := Sys.Process('NOTEPAD');
      Sys.Keys('~of');
      w := Sys.Process('NOTEPAD').Window('#32770', 'Font', 1);
      ComboBox := w.Window('ComboBox', '', 3);
       
      // Select the item
      ComboBox.Keys('14');
    end;

    C++Script, C#Script

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

      // Run Notepad
      WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);
      
      // Obtain a combo box object
      p = Sys["Process"]("NOTEPAD");
      Sys.Keys("~of");
      w = Sys["Process"]("NOTEPAD").Window("#32770", "Font", 1);
      ComboBox = w["Window"]("ComboBox", "", 3);
        
      // Select the item
      ComboBox["Keys"]("14");
    }

    Note: Simple and drop-down combo boxes allow you to not only select predefined items, but also to type arbitrary text. See Combo Box Types in Desktop Windows Applications to get detailed information on different combo box types.
  • You can also navigate within the list of combo box items using keyboard shortcuts, and select the needed item by simulating the Up and Down arrow keys. Here is an example:

    JavaScript, JScript

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

      // Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL);

      // Obtain a combo box object
      p = Sys.Process("NOTEPAD");
      Sys.Keys("~of");
      w = Sys.Process("NOTEPAD").Window("#32770", "Font", 1);
      ComboBox = w.Window("ComboBox", "", 1);
      
      // Select the item
      ComboBox.Keys("[Down][Down][Down][Down][Down]");
    }

    Python

    def Main():
    
      # Run Notepad
      WshShell.Run("notepad.exe", SW_SHOWNORMAL)
    
      # Obtain a combo box object 
      p = Sys.Process("NOTEPAD")
      Sys.Keys("~of")
      w = Sys.Process("NOTEPAD").Window("#32770", "Font", 1)
      ComboBox = w.Window("ComboBox", "", 1)
      
      # Select the item
      ComboBox.Keys("[Down][Down][Down][Down][Down]")

    VBScript

    Sub Main
    Dim p, w, ComboBox

      ' Run Notepad
      Call WshShell.Run("notepad.exe", SW_SHOWNORMAL)
      
      ' Obtain a combo box object
      Set p = Sys.Process("NOTEPAD")
      Sys.Keys "~of"
      Set w = Sys.Process("NOTEPAD").Window("#32770", "Font", 1)
      Set ComboBox = w.Window("ComboBox", "", 1)
      
      'Select the item
      ComboBox.Keys "[Down][Down][Down][Down][Down]"
    End Sub

    DelphiScript

    procedure Main();
    var
       p, w, ComboBox: OleVariant;
    begin
      // Run Notepad
      WshShell.Run('notepad.exe', SW_SHOWNORMAL);
      
      // Obtain a combo box object
      p := Sys.Process('NOTEPAD');
      Sys.Keys('~of');
      w := Sys.Process('NOTEPAD').Window('#32770', 'Font', 1);
      ComboBox := w.Window('ComboBox', '', 1);
       
      // Select the item
      ComboBox.Keys('[Down][Down][Down][Down][Down]');
    end;

    C++Script, C#Script

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

      // Run Notepad
      WshShell["Run"]("notepad.exe", SW_SHOWNORMAL);
      
      // Obtain a combo box object
      p = Sys["Process"]("NOTEPAD");
      Sys.Keys("~of");
      w = Sys["Process"]("NOTEPAD").Window("#32770", "Font", 1);
      ComboBox = w["Window"]("ComboBox", "", 1);
        
      // Select the item
      ComboBox["Keys"]("[Down][Down][Down][Down][Down]");
    }

See Also

Working With Combo Box Controls in Desktop Windows Applications
ClickItem Action (ComboBox Controls)
Simulating Keystrokes
Combo Box Types in Desktop Windows Applications
Working With Combo Box Controls in Web Applications

Highlight search results