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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid

Status
Not open for further replies.

ruyeno22

Programmer
Aug 14, 2001
19
0
0
PA
Hello All,

I want to make the selected row in a datagrid to change when i RIGHT click on it. After that, i want to show a popup menu.
So far i have not seen the way to do it.

Many thans
 
To detect the right mouse button you need to use the MouseUp or MouseDown events. The first argument in either of those events is "Button". You can then do something like
Code:
If Button = vbRightButton Then 
    ' Do Something for a right button click
Else
    ' Do Something Else for the left button.
End If
To pop up a menu ... use the menu editor to create a menu item and un-click the "Visible" check box. Place sub menu entries that you want to appear below it. If you named (for example) the main menu item (i.e. the one that you made invisible) as "mnuSelect" then to pop up the menu on a right click.
Code:
If Button = vbRightButton Then 
    ' Do Something for a right button click
    [b]PopupMenu mnuSelect[/b]
Else
    ' Do Something Else for the left button.
End If
 
hello

thanks for the reply

my problem is that i have a key value in the first column. when i right click on a row, it does not select that row and therefore i cannot catch the value selected.
i want something like in outlook when you right click on emails the email you right click on gets selected and the popup shows the options you have for that email.

i use the mousedown event.
so far i have to:
left click to select the desired value and then
right click to show the popup menu


the click event has no code

the mosedown has code
the rowcolchange has code


 
ruyeno22,

This is what I used:

Code:
Dim blnGridClickSw as Boolean
Dim lngMouseColHold as Long
Dim lngMouseRowHold as Long

Sub Grid1_Click()

'Thow away unwanted click events
    If blnGridClickSw Then Exit Sub

'See if a fixed col or row was clicked
    If lngMouseColHold <= grdDiary.FixedCols - 1 Then Exit Sub
    If lngMouseRowHold <= grdDiary.FixedRows - 1 Then Exit Sub

    blnGridClickSw = True

'Set the selected col and row to where the left or right mouse was clicked
    grdDiary.Col = lngMouseColHold
    grdDiary.Row = lngMouseRowHold

    If blnRightButtonPressed Then
        blnRightButtonPressed = False
        PopupMenu mnuPopUp, vbPopupMenuRightButton
        etc etc for right button
    Else
        etc etc for left button
    End If
End Sub


Private Sub Grid1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    
    lngMouseColHold = grdDiary.MouseCol
    lngMouseRowHold = grdDiary.MouseRow

    If Button = vbRightButton Then
        blnRightButtonPressed = True
    Else
        blnRightButtonPressed = False
    End If

End Sub

My experimentation indicated that although the .MouseCol and .MouseRow properties where set correctly for left button click, the right button click a) did not set the selected Row/Col and b) the .MouseCol and .MouseRow properties where unstable.

Cheers

&quot;Life is full of learning, and then there is wisdom&quot;
 
To force selection of the row before you process
Code:
Private Sub myGrid_MouseDown (Button As Integer, Shift As Integer, X As Single, Y As Single)
[COLOR=green]' A click on the control that is not inside the grid
' will generate an error so you need to trap it.[/color]
On Error GoTo NotOnARow
myGrid.Row = myGrid.RowContaining ( Y )
If Button = vbRightButton Then 
    ' Do Something for a right button click
    PopupMenu mnuSelect
Else
    ' Do Something Else for the left button.
End If
Exit Sub

NotOnARow:

End Sub
 
Hello,

Many thanks!

The options given work like a charm.

Best regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top