Library code snippets

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
               Else
                   found = True
               End If
           Loop
           '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.

Comments

  1. 10 Jul 2003 at 11:23

    While this is an ingenious solution, you can always use the .MouseRow property to detect header clicks.

  2. 01 Jan 1999 at 00:00

    This thread is for discussions of Detecting Column Header clicks for MSFlexGrid.

Leave a comment

Sign in or Join us (it's free).