Getting Combo Box Items

Applies to TestComplete 14.93, last modified on October 12, 2021

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 testing an application that uses combo box controls, you may need to obtain combo box items to process them. The Win32ComboBox object that corresponds to the tested combo box control provides a set of properties and methods that allow you to work with the combo box's individual items. Depending on which item you want to obtain, you can get combo box items in the following ways:

  • You can obtain any item specified by its index via the wItem property. The following code snippet demonstrates how to get an item's 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", "", 5);
      
      // Copy the second item's text to the clipboard
      Sys.Clipboard = ComboBox.wItem(1);
      Log.Message("The second item:" + Sys.Clipboard);
    }

    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)
      
      # Copy the second item's text to the clipboard
      Sys.Clipboard = ComboBox.wItem[1]
      Log.Message("The second item:" + Sys.Clipboard)

    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)
      
      ' Copy the second item's text to the clipboard
      Sys.Clipboard = ComboBox.wItem(1)
      Log.Message("The second item:" & Sys.Clipboard)
    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);
      
      // Copy the second item's text to the clipboard
      Sys.Clipboard := ComboBox.wItem(1);
      Log.Message('The second item:' + Sys.Clipboard);
    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);
      
      // Copy the second item's text to the clipboard
      Sys["Clipboard"] = ComboBox["wItem"](1);
      Log["Message"]("The second item:" + Sys["Clipboard"]);
    }

    Note: Enumeration of combo box items is zero-based, that is, the index of the first item is 0.
  • When you need to get the currently selected item, you can determine its index via the wSelectedItem property and then obtain the item's text via the wItem property. Or you can use the wText property that returns the text of the currently selected item. Here is an example:

    JavaScript, JScript

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

      // 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 an item
      ComboBox.Keys("[Down][Down]");
      
      // Obtain the selected item by its index
      ItemIndex = ComboBox.wSelectedItem;
      SelItem = ComboBox.wItem(ItemIndex);
      Log.Message(SelItem);
        
      // Obtain the selected item's text via the wText property
      Sys.Clipboard = ComboBox.wText;
      Log.Message(Sys.Clipboard);
    }

    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 an item
      ComboBox.Keys("[Down][Down]")
      
      # Obtain the selected item by its index
      ItemIndex = ComboBox.wSelectedItem
      SelItem = ComboBox.wItem[ItemIndex]
      Log.Message(SelItem)
        
      # Obtain the selected item's text via the wText property
      Sys.Clipboard = ComboBox.wText
      Log.Message(Sys.Clipboard)

    VBScript

    Sub Main
    Dim p, w, ComboBox, ItemIndex,SelItem

      ' 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 an item
      ComboBox.Keys "[Down][Down]"
      
      ' Obtain the selected item by its index
      ItemIndex = ComboBox.wSelectedItem
      SelItem = ComboBox.wItem(ItemIndex)
      Log.Message(SelItem)
        
      ' Obtain the selected item's text via the wText property
      Sys.Clipboard = ComboBox.wText
      Log.Message(Sys.Clipboard)
    End Sub

    DelphiScript

    procedure Main();
    var
    p, w, ComboBox, ItemIndex,SelItem: 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 an item
      ComboBox.Keys('[Down][Down]');
      
      // Obtain the selected item by its index
      ItemIndex := ComboBox.wSelectedItem;
      SelItem := ComboBox.wItem(ItemIndex);
      Log.Message(SelItem);
        
      // Obtain the selected item's text via the wText property
      Sys.Clipboard := ComboBox.wText;
      Log.Message(Sys.Clipboard);
    end;

    C++Script, C#Script

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

      // 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 an item
      ComboBox["Keys"]("[Down][Down]");
      
      // Obtain the selected item by its index
      ItemIndex = ComboBox["wSelectedItem"];
      SelItem = ComboBox["wItem"](ItemIndex);
      Log["Message"](SelItem);
        
      // Obtain the selected item's text via the wText property
      Sys["Clipboard"] = ComboBox["wText"];
      Log["Message"](Sys["Clipboard"]);
    }

  • If you want to get all of the items displayed in the combo box, use the wItemList property. This property returns a string that contains all of the combo box items separated by a delimiter. A delimiter can contain several symbols as well as one symbol. You can get or set them via the wListSeparator property of the Win32ComboBox object. Thus, after determining the delimiter, you can extract individual items from the obtained string, as shown in the example below:

    JavaScript, JScript

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

      // 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);
      
      // Obtain the list of items and the current delimiter
      ItemString = ComboBox.wItemList;
      Del = ComboBox.wListSeparator;
      aqString.ListSeparator = Del;
      
      // Extract particular items and post them to the log
      for (i = 0; i < ComboBox.wItemCount; i++){
        Item = aqString.GetListItem(ItemString, i);
        Log.Message(Item);}
    }

    Python

    def Main3():
    
      # 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)
      
      # Obtain the list of items and the current delimiter
      ItemString = ComboBox.wItemList
      Del = ComboBox.wListSeparator
      aqString.ListSeparator = Del
      
      # Extract particular items and post them to the log 
      for i in range(0, ComboBox.wItemCount-1):
        Item = aqString.GetListItem(ItemString, i)
        Log.Message(Item)

    VBScript

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

      ' 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)
      
      ' Obtain the list of items and the current delimiter
      ItemString = ComboBox.wItemList
      Del = ComboBox.wListSeparator
      aqString.ListSeparator = Del
      
      ' Extract particular items and post them to the log
      for i = 0 to ComboBox.wItemCount-1
        Item = aqString.GetListItem(ItemString, i)
        Log.Message(Item)
      next
    End Sub

    DelphiScript

    procedure Main();
    var
    p, w, i, ComboBox, Item, ItemString, Del: 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);
      
      // Obtain the list of items and the current delimiter
      ItemString := ComboBox.wItemList;
      Del := ComboBox.wListSeparator;
      aqString.ListSeparator := Del;
      
      // Extract particular items and post them to the log
      for i := 0 to ComboBox.wItemCount-1 do
      begin
        Item := aqString.GetListItem(ItemString, i);
        Log.Message(Item);
      end
    end;

    C++Script, C#Script

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

      // 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);
      
      // Obtain the list of items and the current delimiter
      ItemString = ComboBox["wItemList"];
      Del = ComboBox["wListSeparator"];
      aqString["ListSeparator"] = Del;
      
      // Extract particular items and post them to the log
      for (i=0; i<ComboBox["wItemCount"]; i++)
      {
        Item = aqString["GetListItem"](ItemString, i);
        Log["Message"](Item);
      }
    }

See Also

Working With Combo Box Controls
wItem Property (ComboBox Controls)
wText Property (ComboBox Controls)
wItemList Property
wSelectedItem Property (Combo Box Controls)
wListSeparator Property

Highlight search results