Retrieving Data From Microsoft FlexGrid. Example

Applies to TestComplete 14.40, last modified on April 22, 2021

This example illustrates how you can obtain data from the cells of an MS FlexGrid control in a Visual Basic Open Application. It requires a form with a FlexGrid control on it.

The following procedure scans the grid rows and posts the contents of the column specified by its name to the test log:

JavaScript, JScript

function TestFlexGrid(ColumnName)
{
  var p, w, ColIdx, i, s;

  p = Sys.Process("MyProject");
  w = p.VBObject("Form1").VBObject("MSFlexGrid1");
  // Search for the column index
  ColIdx = -1;
  for (i = 0; i < w.Cols; i++)
    if ( w.TextMatrix(0, i) == ColumnName)
    {
      ColIdx = i;
      break;
    }
  // If the column was not found...
  if (ColIdx == -1)
    // Post warning to the test log
    Log.Warning("Column '" + ColumnName + "' was not found.");
  else
    // Retrieve data from the column cells
    for (i = 1; i < w.Rows; i++)
    {
      s = w.TextMatrix(i, ColIdx);
      Log.Message(s);
    }
}

Python

def TestFlexGrid(ColumnName):

  p = Sys.Process("MyProject")
  w = p.VBObject("Form1").VBObject("MSFlexGrid1")
  # Search for the column index
  ColIdx = -1
  for i in range (0, w.Cols):
    if (w.TextMatrix[0, i] == ColumnName):
      ColIdx = i
      break
  # If the column was not found...
  if (ColIdx == -1):
    # Post warning to the test log
    Log.Warning("Column '" + ColumnName + "' was not found.")
  else:
    # Retrieve data from the column cells
    for i in range (1, w.Rows):
      s = w.TextMatrix[i, ColIdx]
      Log.Message(s)

VBScript

Sub TestFlexGrid(ColumnName)
  Dim p, w, ColIdx, i, s

  Set p = Sys.Process("MyProject")
  Set w = p.VBObject("Form1").VBObject("MSFlexGrid1")
  ' Search for the column index
  ColIdx = -1
  For i = 0 To w.Cols - 1
    If w.TextMatrix(0, i) = ColumnName Then
      ColIdx = i
      Exit For
    End If
  Next
  ' If the column was not found...
  If ColIdx = -1 Then
    ' Post warning to the test log
    Log.Warning "Column '" & ColumnName & "' was not found."
  Else
    ' Retrieve data from the column cells
    For i = 1 To w.Rows - 1
      s = w.TextMatrix(i, ColIdx)
      Log.Message s
    Next
  End If
End Sub

DelphiScript

procedure TestFlexGrid(ColumnName : OleVariant);
var
  p, w, ColIdx, i, s : OleVariant;
begin
  p := Sys.Process('MyProject');
  w := p.VBObject('Form1').VBObject('MSFlexGrid1');
  // Search for the column index
  ColIdx := -1;
  for i := 0 to w.Cols - 1 do
    if w.TextMatrix(0, i) = ColumnName then
    begin
      ColIdx := i;
      break;
    end;
  // If the column was not found...
  if ColIdx = -1 then
    // Post warning to the test log
    Log.Warning('Column ''' + ColumnName + ''' was not found.')
  else
    // Retrieve data from the column cells
    for i := 1 to w.Rows - 1 do
    begin
      s := w.TextMatrix(i, ColIdx);
      Log.Message(s);
    end;
end;

C++Script, C#Script

function TestFlexGrid(ColumnName)
{
  var p, w, ColIdx, i, s;

  p = Sys["Process"]("MyProject");
  w = p["VBObject"]("Form1")["VBObject"]("MSFlexGrid1");
  // Search for the column index
  ColIdx = -1;
  for (i = 0; i < w["Cols"]; i++)
    if ( w["TextMatrix"](0, i) == ColumnName)
    {
      ColIdx = i;
      break;
    }
  // If the column was not found...
  if (ColIdx == -1)
    // Post warning to the test log
    Log["Warning"]("Column '" + ColumnName + "' was not found.");
  else
    // Retrieve data from the column cells
    for (i = 1; i < w["Rows"]; i++)
    {
      s = w["TextMatrix"](i, ColIdx);
      Log["Message"](s);
    }
}

See Also

Working With Grids
Testing Visual Basic Applications - Overview

Highlight search results