Clicking Within a List Box Item in Desktop Windows Applications

Applies to TestComplete 15.40, last modified on July 05, 2022

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.

In your tests you may need to perform a click within a list box's item. If the control that you are working with is a standard Windows list box control, you can use specific properties and methods provided by the Win32ListBox object, which corresponds to this control. Thus, you can use the ClickItemXY action to simulate a click on the specified point of an item. You should specify the item, on which you want to click, and the coordinates of a needed point within the item.

The click point must be specified in item-relative coordinates. You can determine the bounds of an item’s rectangle as it is described in the Determining List Box Item Bounds in Desktop Windows Applications topic. Then you can use this information to obtain the item’s width and height and to estimate the coordinates of the click.

The following code example demonstrates how to simulate a mouse click on a point within an item:

JavaScript, JScript

function Main()
{
  var p, w, ListBox, Size, x, y;

  // 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 item bounds and specify the point to click
  Size = ListBox.wItemBounds(0);
  x = Size.Width - 10;
  y = Size.Height - 5;

  // Click on the specified point
  ListBox.ClickItemXY(0, x, y);
}

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 item bounds and specify the point to click 
  Size = ListBox.wItemBounds[0]
  x = Size.Width - 10
  y = Size.Height - 5

  # Click on the specified point
  ListBox.ClickItemXY(0, x, y)

VBScript

Sub Main
  Dim p, w, ListBox, Size, x, y

  ' 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 item bounds and specify the point to click
  Set Size = ListBox.wItemBounds(0)
  x = Size.Width - 10
  y = Size.Height - 5

  ' Click on the specified point
  ListBox.ClickItemXY 0, x, y
End Sub

DelphiScript

procedure Main;
var p, w, ListBox, Size, x, y : 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 item bounds and specify the point to click
  Size := ListBox.wItemBounds[0];
  x := Size.Width - 10;
  y := Size.Height - 5;

  // Click on the specified point
  ListBox.ClickItemXY(0, x, y);
end;

C++Script, C#Script

function Main()
{
  var p, w, ListBox, Size, x, y;

  // 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 item bounds and specify the point to click
  Size = ListBox.wItemBounds(0);
  x = Size["Width"] - 10;
  y = Size["Height"] - 5;

  // Click on the specified point
  ListBox["ClickItemXY"](0, x, y);
}

See Also

Working With List Box Controls in Desktop Windows Applications
ClickItemXY Action (ListBox Controls)
Determining List Box Item Bounds in Desktop Windows Applications
Working With List Box Controls in Web Applications

Highlight search results