Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid HeadClick - right or left mouse button 3

Status
Not open for further replies.

MangroBongacello

Programmer
Mar 20, 2001
189
0
0
SI
Dear friends,

is it possible to know, whith which mouse button the column header of a data grid was clicked (in the HeadClick event). Or, if someone has a better idea to achieve sorting in a grid. My idea is, if a user clicks left button it means ascending order, and if clicks right button, it means descending order. Any help would be great.

Thank you, Mangro
 
Does this help.....

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

If Button = 1 Then
MsgBox "Left"
ElseIf Button = 2 Then
MsgBox "Right"
End If

End Sub
 
Thank you for reply, kmcclung, but I wish to do sorting on a grid when the user clicks column header and not anywhere on a grid, so the appropriate event would be HeadClick, which unfortunately doesn't have the Button argument. Perhaps any other idea?

Mangro
 
The grid mouse down event should fire before the HeadClick event. If that is the case set a boolean flag in the mouse down to determine which button is being used and then have a selection in the headclick event that is based on the button flag. A modular level flag would work.

private m_intButtonUsed as integer 'modular or form declaration

Private Sub grid_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
m_intButtonUsed=Button
End Sub

Private sub grid_HeadClick()
If m_intButtonUsed = 1 Then
MsgBox "Left"
ElseIf m_intButtonUsed = 2 Then
MsgBox "Right"
End If
end sub

This is probvably not the cleanest way to accomplish this. But it is a starting point.
Thanks and Good Luck!

zemp
 
To do what you want, use the grid's Mouse Events and the ColContaining/RowContaining methods:

Option Explicit
Private Enum eGrdMouseButton
gmbNone = 0
gmbRight
gmbLeft
gmbMiddle
End Enum
Private GridMouseButtonUsed As eGrdMouseButton

Private Sub DataGrid1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim iButtonRight As Integer
Dim iButtonLeft As Integer
Dim iCol As Integer
Dim iRow As Integer

iButtonRight = (Button And vbRightButton) > 0
iButtonLeft = (Button And vbLeftButton) > 0

iCol = DataGrid1.ColContaining(X)
iRow = DataGrid1.RowContaining(Y)

If iRow = -1 And iCol >= 0 Then
If iButtonRight Then
DataGrid1.SelStartCol = iCol
GridMouseButtonUsed = gmbRight
ElseIf iButtonLeft Then
GridMouseButtonUsed = gmbLeft
End If
End If

End Sub

Private Sub DataGrid1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If GridMouseButtonUsed Then DataGrid1_HeadClick DataGrid1.ColContaining(X)
End Sub

Private Sub DataGrid1_HeadClick(ByVal ColIndex As Integer)

If GridMouseButtonUsed = gmbLeft Then
Debug.Print "Sort ASC"
DataGrid1.SelStartCol = -1
ElseIf GridMouseButtonUsed = gmbRight Then
Debug.Print "Sort DESC"
DataGrid1.SelStartCol = -1
Else
Debug.Print "Nothing done"
End If

GridMouseButtonUsed = gmbNone
End Sub

Private Sub DataGrid1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
GridMouseButtonUsed = gmbNone
End Sub

There is some extra code to help take care of situations where a user may be just marking columns
(marking a single column may still not work as expected), and additional code to make the left and
right clicks act the same (otherwise, the left click marks the column blue and the right click doesn't)
[/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
yeah, well... zemp, this seemed to be a solution, but, hm...I never checked it before... the HeadClick event doesn't fire when using right button. Never mind. I now did it like this: when the user clicks the first time on a column header, the records are sorted in ascending order, and the second time in descending order. I guess it will be good enough.

Thank you anyway, Mangro
 
Thank you, CCLINT, I knew I can count on you. Just what I need. Here's a star. Thanks again,

Mangro
 

MangroBongacello:
You're welcome and thank you!

zemp also had the right idea (capture the click on the mouse down). Just needed to be extended on the idea.
I just happened to have something already worked out, and posted, prior to seeing zemp's post, otherwise I may have left it.

>when the user clicks the first time on a column header, >the records are sorted in ascending order, and the second >Ytime in descending order.

Actually, I do it similar to the way you mentioned above also, in addition to allowing the user to select sorting over a toolbar icon and menu point.

Doing it on mouse right and left clicks conflicts with other standards such as pop-up menus and column marking (just marking a single column with-out wanting to sort will not work unless the user moves the mouse a tad while doing so)

[/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top