Detecting Column Header clicks for MSFlexGrid

To use this code, add a flexgrid control to a form, and add the following to the MouseDown or MouseUp event of the control.

Private Sub MSFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

Dim cont As Integer
Dim found As Boolean

   'Only do it if the button is the left one, and there
   'are no mask keys pressed.
   'Of course, you can vary this to fit your needs.
   If (Button = vbLeftButton) And (Shift = 0) Then
       With MSFlexGrid1
           'Do not proceed if the row clicked is
           'not the header row.
           If (.RowHeight(0) < y) Then
               Exit Sub
           End If
           'Initialize variables
           cont = 0
           found = False
           'Find the column clicked using mouse coords
           Do While (cont < .Cols) And Not (found)
               If (.ColPos(cont) + .ColWidth(cont) < x) Then
                   cont = cont + 1
                   found = True
               End If
           'If column found, proceed to run the appropriate code
           If found Then
               MsgBox "You clicked the header for column " & .TextMatrix(0, cont)
           End If
       End With
   End If
End Sub

The above will work for one fixed row only.  However, you can easily adapt it to multiple fixed header rows.

You might also like...


Jose Pablo Ramirez Vargas


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Walking on water and developing software from a specification are easy if both are frozen.” - Edward V Berard