Working With TDBGrid - Simulating Click On Column Headers
Test and Monitor | Posted April 08, 2007

When testing applications that use Borland TDBGrid controls, you may need to simulate mouse clicks on a column header. For example, in this way you can sort the grid data, if sorting is enabled. This article explains how to do that.

You can simulate a click by using the Click action of the scripting object that corresponds to the TDBGrid control. This action uses three parameters: ClientX, ClientY and Shift. The Shift parameter specifies whether the CTRL, SHIFT or ALT key should be pressed while clicking. ClientX and ClientY specify the coordinates for the click. Your script should calculate the coordinates before simulating the click.

The coordinates are relative to the grid window. To find them, you should add the width of grid columns. To obtain the column width, you can use the ColWidths property of the TDBGrid control. This is an internal property and in order to enable TestComplete to access it, the tested application must be compiled as an Open Application with debug information.

The script code below demonstrates how to simulate a mouse click on the column specified by its caption. The sample contains the following routines:

  • Main - The “main” routine of the sample. It obtains the scripting object that corresponds to the tested TDBGrid control and then calls the ClickHeader routine that simulates a mouse click on the column header.
  • ClickHeader - Simulates a click of the left mouse button over the specified column header. The routine uses two parameters: GridObject and ColumnCaption. The GridObject parameter specifies the scripting object that corresponds to the TDBGrid control, ColumnCaption holds the caption of the desired column.

    The horizontal coordinate is stored in the X variable. To find this coordinate, we should add the width of all columns that precede the desired column in the grid. To obtain the column width, we use the ColWidths(Index) property of the TDBGrid object (Index specifies the index of the desired column in the grid). In fact, ColWidths is a collection of columns' widths.

    The indicator width (if the indicator is visible) is specified by ColWidths(0). To determine whether the indicator is visible, we use the IndicatorOffset property of the TDBGrid object. If this property is greater than 0, the indicator is visible. Else, the indicator is hidden.

    The routine goes through the collection of all grid columns and compares the column caption with the specified caption. To obtain the column caption, first, we get the TColumn object via the GridObject.Columns.Items(Index) property and then we use the DisplayName property of this object. If the column caption differs from the specified caption, we add the column’s width to X. If the column caption coincides with the specified caption, we add 4 pixels to X (this is done to ensure that the click will be performed over the desired column header) and then exit the loop.

    Note that the ColWidths collection includes an item for the indicator width (if the indicator is visible) while the Columns.Item collection does not provide access to the indicator. The indexes that are used to retrieve values from these collections may differ from the value of the IndicatorOffset property.

    The routine does not calculate the vertical coordinate of the click. Since the column headers always resides at the top of the control, we use a reasonably small value, 4, as the vertical coordinate. This should guarantee that the click will be performed within the header area.

    For simplicity, the routine does not include the code that checks whether the specified column is found.


By submitting this form, you agree to our
Terms of Use and Privacy Policy

Thanks for Subscribing

Keep an eye on your inbox for more great content.

Continue Reading

Add a little SmartBear to your life

Stay on top of your Software game with the latest developer tips, best practices and news, delivered straight to your inbox