Determining Current List Box Item

Applies to TestComplete 14.0, last modified on January 23, 2019

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.

Working with a list box control you may need to know which item is currently focused. If the control that you are testing is a standard Windows list box control, TestComplete associates it with the Win32ListBox object, which corresponds to this control. Thus, you can use properties and methods provided by Win32ListBox to work with a list box control and its individual items.

You can determine the current item by using the wFocus property. This property returns the index of the item, which is currently focused. The following code example checks whether the specified list box item is focused:

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

  // Check whether the first item gets focus
  if (ListBox.wFocus == 0)
    Log.Message("The first item is currently focused.")
  else
    Log.Message("The first item is not focused.");
}

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) 

  # Check whether the first item gets focus
  if ListBox.wFocus == 0:
    Log.Message("The first item is currently focused.")
  else:
    Log.Message("The first item is not focused.")

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)

  ' Check whether the first item gets focus
  If ListBox.wFocus = 0 Then
    Log.Message("The first item is currently focused.")
  Else
    Log.Message("The first item is not focused.")
  End If 
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);

  // Check whether the first item gets focus
  if ListBox.wFocus = 0 then
    Log.Message('The first item is currently focused.')
  else
    Log.Message('The first item is not focused.')
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);

  // Check whether the first item gets focus
  if (ListBox.wFocus == 0)
    Log.Message("The first item is currently focused.")
  else
    Log.Message("The first item is not focused.")
}

Note: Numeration of the list box’s items is zero-based, that is the index of the first item is 0.

See Also

Working With List Box Controls
wFocus Property (ListBox Controls)

Highlight search results