VB6 - (DataGrid) Get the Row selected with the right mouse button-VBForums
Results 1 to 1 of 1

Thread: VB6 - (DataGrid) Get the Row selected with the right mouse button

  1. #1

    Thread Starter
    Addicted Member Optional's Avatar
    Join Date
    Jan 2010
    Location
    Rudimentary Space
    Posts
    214

    VB6 - (DataGrid) Get the Row selected with the right mouse button

    I found, using a DataGrid control in VB6 there is no property you can query in any of the events which tells you the row you have clicked on when you are using the right button of the mouse.

    You might need that for right-click menus or other features you would like to add.

    The following method will determine which of the rows you have clicked on (using the right mouse button), derived from the Y-Coordinates as passed for example from the Mouse_Down event.

    After the code below I go more into detail why I'm using certain parameters and logic. I have used the shown method in several applications and never had any incorrect rows returned or users reporting any issues.

    VB Code:
    1. '/// <summary>Determines the selected row, derived from the Y position in the grid.</summary>
    2. '/// <param name="gridRowHeight">The height of the rows in the grid.</param>
    3. '/// <param name="gridBorderWidth">The width of the border between the rows.</param>
    4. '/// <param name="y">The y-coordinate the mouse was clicked within the grid.</param>
    5. '/// <param name="gridHasHeaderRow">Indicates whether the grid has a header row or not.</param>
    6. '/// <param name="headerLineCount">The count of header-lines on the grid.</param>
    7. '/// <returns>
    8. '/// Returns the positional number of the selected row or -1 if the
    9. '/// selection (y) was made in an invalid area or an error has occurred.
    10. '/// </returns>
    11. Public Function GetSelectedDataGridRowByY( _
    12.     gridRowHeight As Integer, gridBorderWidth As Integer, y As Single, _
    13.     gridHasHeaderRow As Boolean, headerLineCount As Integer) As Integer
    14.    
    15.     '/// <summary>Stores the return value.</summary>
    16.     Dim returnValue As Integer
    17.    
    18.     '/// <summary>Stores the height of the header row.</summary>
    19.     Dim headerRowHeight As Integer
    20.  
    21.     '/// <summary>Stores selected row, not taking into account any borders.</summary>
    22.     Dim approximateSelectedRow As Double
    23.  
    24.     '/// <summary>Stores the number of borders to deduct.</summary>
    25.     Dim numberOfBorders As Integer
    26.  
    27.     '/// <summary>Stores the actual selected row after borders have been deducted.</summary>
    28.     Dim exactSelectedRow As Integer
    29.  
    30.     '/// <summary>The exact Y value after deduction borders and header row.</summary>
    31.     Dim exactY As Integer
    32.    
    33.     If gridHasHeaderRow Then
    34.         headerRowHeight = (225 * headerLineCount)
    35.         exactY = (y - headerRowHeight)
    36.     Else
    37.         exactY = y
    38.     End If
    39.  
    40.     approximateSelectedRow = (exactY / gridRowHeight)
    41.  
    42.     numberOfBorders = Round(approximateSelectedRow - 0.5)
    43.  
    44.     approximateSelectedRow = _
    45.         ((exactY - ((numberOfBorders) * gridBorderWidth)) / gridRowHeight)
    46.  
    47.     exactSelectedRow = Round(approximateSelectedRow - 0.5)
    48.  
    49.     If ((exactSelectedRow >= 0) And (exactY >= 0)) Then
    50.         returnValue = exactSelectedRow
    51.     Else
    52.         returnValue = -1
    53.     End If
    54.  
    55. Finally:
    56.     GetSelectedDataGridRowByY = returnValue
    57.     Exit Function
    58.  
    59. Catch:
    60.     returnValue = -1
    61.     Resume Finally
    62. End Function

    This is how you might use the code:
    VB Code:
    1. '/// <summary>
    2. '/// Occurs when mouse has been pressed down on DataGrid1.
    3. '/// </summary>
    4. Private Sub DataGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    5.     If Button = vbRightButton Then
    6.         '/// <summary>Stores the positional number of the selected row.</summary>
    7.         Dim newRow As Integer
    8.        
    9.         '/// <summary>Stores the borderwidth of the grid-lines in the DataGrid.</summary>
    10.         Const borderWidthInTwips As Integer = 13
    11.        
    12.         newRow = GetSelectedDataGridRowByY(DataGrid1.RowHeight, borderWidthInTwips, y, True, DataGrid1.HeadLines)
    13.        
    14.         MsgBox "Row number: " & newRow & " has been clicked."
    15.     End If
    16. End Sub

    Code Details
    The basic idea is to take the y-coordinate the mouse has been clicked on in the DataGrid and divide that number by the height of the rows. Just doing that though will create discrepancies in DataGrids with a lot of Rows. This is due to the border in-between each row. In addition I decided not to include the header row in the process, if it has been indicated that a header row exists to ensure the header stays neutral. Feel free though to add a parameter to allow the developer to specify this generically instead.

    The code above will re-calculate the y-coordinate to the “true” y coordinates in case a header has been included and selected. In addition the number of borders are determined and their height is deducted from y as well. Thus at the end the exact y coordinate, divided by the height of the rows will get the exact row selected.

    Additional Notes
    A note on the headerLineCount parameter and the headerRowHeight variable. There is a property on the DataGrid named “HeadLines” which determines how many rows high the header-row is. The exact row height seems fixed though, meaning the header row will always be 225 twips (15pixels) per HeadLine.

    Another note on the '13' borderwidth; That is what worked best for me as border width. The borderwidth in a DataGrid seems to be just under a full pixel. A full pixel is 15 twips but I did get slight discrepencies when clicking near borders further down the grid. This however could have been a graphical glitch. If 15 works for you better then use 15 instead.
    Last edited by Optional; Mar 5th, 2010 at 08:35 AM.



    Kind Regards,
    Optional



    If you feel this post has helped in answering your question please return the favour and Rate this post.
    If your problem has been solved and your question has been answered mark the thread as [RESOLVED] by selecting the Thread Tools menu option at the top and clicking the Mark Thread Resolved menu item.


    VB6 - (DataGrid) Get the Row selected with the right mouse button



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.