Forum Discussion

vthomeschoolmom's avatar
vthomeschoolmom
Super Contributor
13 years ago

TrueDBGridPor 8 calulating the coordinates of a CELL

Hiya. A helpful soul shared a solution for calculating the coordinates of a row selector in a grid here.



http://smartbear.com/forums/forum/post/?mode=singlethread&thread=f3152b4a-9cfa-4de1-a607-18c22c903b5c



Thanks, Helen!



I am looking for something similar to click in a cell. I am not seeing properties that I could use for this. I wonder if someone else with experience with this has worked it out?



Thanks







(Note this is TDBGrid Pro 8 by Component One for VB 6.)
  • Julia_K's avatar
    Julia_K
    SmartBear Alumni (Retired)

    Hello Lane,


    To calculate coordinates of the TrueDBGrid cells, you can use the Left, Width and RowHeight properties and the RowTop method of the grid.


    For example, you can modify the code Helen posted in the following way:



    ' Calculates coordinates of a grid cell by its row index and column index or caption

    ' And simulates a click in the specified cell



    Sub ClickCell(grid, rowIndex, column)

    Dim SplitNo, SplitCount



    ' Checks whether the grid contains the specified row

      If rowIndex < 1 Or rowIndex > grid.ApproxCount Then

        Log.Error "Row index " & rowIndex & " is out of bounds 1 ... " & grid.ApproxCount & "."

        Exit Sub

      End If



      ' Checks whether the grid contains the specified column

      If Not aqObject.GetVarType(column) = 8 Then

        If column < 0 Or column >= grid.Columns.Count Then

          Log.Error "Column index " & column & " is out of bounds 0 ... " & grid.Columns.Count & "."

          Exit Sub

       End If

      Else

        Err.Clear

        On Error Resume Next

          Set col = grid.Columns(column)

        If Err.Number <> 0 Then

          Log.Error Err.Description

          Exit Sub

        End If  

        

      End If



      ' Checks whether the row is visible

      If (grid.VisibleRows + grid.FirstRow) < rowIndex Or grid.FirstRow > rowIndex Then

        ' Scrolls to the specified row

        grid.FirstRow = rowIndex

        ' Obtains the index of the specified row among the visible rows

        rowIndexV = 0

      Else

        ' Obtains the index of the specified row among the visible rows

        rowIndexV = rowIndex - grid.FirstRow

      End If  



      ' Obtains the number of splits in the grid

      SplitCount = grid.Splits.Count

      SplitNo = 0



      For i = 0 To SplitCount - 2

       columnIndex = grid.Splits(SplitNo).Columns(column).ColIndex

        If columnIndex >= grid.Splits(i).LeftCol And columnIndex < grid.Splits(i+1).LeftCol Then

          SplitNo = i

        Else

          SplitNo = grid.Splits.Count - 1 

        End If

      Next



      ' Checks whether the column is visible

      FirstVisisble = grid.Splits(SplitNo).LeftCol

      LastVisible = grid.Splits(SplitNo).LeftCol + grid.VisibleCols - 1

      If LastVisible <= grid.Splits(SplitNo).Columns(column).ColIndex _ 

       Or FirstVisible >= grid.Splits(SplitNo).Columns(column).ColIndex Then

        ' Scrolls to the specified column

        grid.Splits(SplitNo).LeftCol = grid.Splits(SplitNo).Columns(column).ColIndex

      End If



      ' Calculates the horizontal coordinates of the cell (in VB twips)

      xTwips = grid.Splits(SplitNo).Columns(column).Left + (grid.Splits(SplitNo).Columns(column).Width / 2)

      ' Converts twips to pixels

      x = CLng(TwipsToPixelsX(xTwips))



      ' Calculates the vertical coordinates of the cell (in VB twips)

      yTwips = grid.RowTop(rowIndexV) + grid.RowHeight / 2

      ' Converts twips to pixels

      y = CLng(TwipsToPixelsY(yTwips))



      ' Clicks the specified cell

      Log.Message "Clicking cell (" & rowIndex & ", " & column & ")"

      grid.Click x, y



    End Sub



    Const TWIPSPERINCH = 1440



    Function TwipsToPixelsX(XTwips)

      Dim Xdpi

      Xdpi = Win32API.GetDeviceCaps(Win32API.GetDC(0), Win32API.LOGPIXELSX)

      TwipsToPixelsX = XTwips / TWIPSPERINCH * Xdpi

    End Function



    Function TwipsToPixelsY(YTwips)

      Dim Ydpi

      Ydpi = Win32API.GetDeviceCaps(Win32API.GetDC(0), Win32API.LOGPIXELSY)

      TwipsToPixelsY = YTwips / TWIPSPERINCH * Ydpi

    End Function



    ' Obtains the grid and simulates clicks on its cells

    Sub Test

      Dim grid



      Set grid = Sys.Process("Ub2RsCls").VBObject("UB2ADO").VBObject("TDBGrid1") 



      ClickCell grid, 10, 3

      ClickCell grid, 25, "Notes"

      ClickCell grid, 100, "Comments"



    End Sub


    You can use this sample code as a base for your own code that suits your needs better.

    Please let me know whether this information helps, or whether you have any additional questions.


    Thanks you.

  • I will try this and let you know the results. I appreciate the help very much. Thanks



    S