Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically what I have is a Datagridview that I want to show a context menu strip when it is right clicked. But only when right clicked on a cell, not on the header or a blank part of the datagridview control. I also want it to select the entire row when right clicked. I have the row selection working, and the context menu strip opens but it open way away from the mouse as per the screenshot.

VB
Private Sub DgvDelays_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DgvDelays.CellMouseDown
    Dim rowClicked As DataGridView.HitTestInfo = DgvDelays.HitTest(e.X, e.Y)
    'Select Right Clicked Row if its not the header row
    If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex > -1 Then
        'Clear any currently sellected rows
        DgvDelays.ClearSelection()
        Me.DgvDelays.Rows(e.RowIndex).Selected = True
        CmsRightClick.Show(DgvDelays, New System.Drawing.Point(Windows.Forms.Cursor.Position))

    End If

End Sub


Image[^]
Posted
Updated 10-Dec-17 16:30pm
Comments
Sergey Alexandrovich Kryukov 31-Dec-12 0:57am    
System.Windows.Forms? Tag it!
—SA
Sergey Alexandrovich Kryukov 31-Dec-12 1:00am    
So, is the location of context menu string the only problem? What is the line of code showing it? CmsRightClick.Show?
—SA
brzt 31-Dec-12 1:06am    
Yes just the location that its loading at, I have included all my code for the sub as I was thinking it might be part of the problem.

CmsRightClick.Show(DgvDelays, New System.Drawing.Point(Windows.Forms.Cursor.Position))

^^Code That Shows The Menu^^
Sergey Alexandrovich Kryukov 31-Dec-12 1:21am    
OK, that's what I thought. Then I have a solution for you; please see Solution 1.
Hope it works for you, so you could formally accept it (green button).
—SA
brzt 31-Dec-12 2:57am    
Thanks Mate,
Heading out for NYE Celebrations. Will Check and confirm it as working tomorrow and accept the solution if it is. Thank you :D

Please see my comments to the question — I'm not 100% sure I understand your code, because incomplete code is shown. If some of my assumptions are wrong, we can sort things out if you clarify it.

It looks like you obtain cursor position in screen coordinates; and the menu is created in control coordinates. You need to add appropriate shift. One of the ways to do it is the pair of methods: System.Windows.Forms.Control.PointToClient and System.Windows.Forms.Control.PointToScreen. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoclient.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoscreen.aspx[^].

Another approach: don't use Windows.Forms.Cursor.Position. Obtain mouse coordinates in control's coordinate system in first place, by handling the event MouseUp of this control.

[EDIT]

Don't mix up different Show methods.

These two show in screen coordinates:
http://msdn.microsoft.com/en-us/library/xz0wbhy2.aspx[^],
http://msdn.microsoft.com/en-us/library/7z41k67k.aspx[^].

These two show relative to the control passes as first parameter:
http://msdn.microsoft.com/en-us/library/71tzk45y.aspx[^],
http://msdn.microsoft.com/en-us/library/15e8969w.aspx[^].

In first case, you can use Windows.Forms.Cursor.Position, in second case — coordinates passed in the parameter e. Use some extra attention; it's easy to make a mistake.

Good luck,
—SA
 
Share this answer
 
v3
Ok I Have Found A Solution, using to publicly shared Variables, then storing the location the mouse is clicked when it is clicked any where on the control but only running the show context menu when the mouse down is on a cell.

Very Big Thank you to Sergey Alexandrovich Kryukov, for working through this with me


VB
Public Shared MouseX As Integer
Public Shared MouseY As Integer


Then Use The Following Code For The Mouse Down On The Cell

VB
Private Sub DgvDelays_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DgvDelays.MouseDown
    MouseX = e.X
    MouseY = e.Y
End Sub


And the Following Code In The Cell Mouse Down

VB
Private Sub DgvDelays_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DgvDelays.CellMouseDown
    Dim rowClicked As DataGridView.HitTestInfo = DgvDelays.HitTest(e.X, e.Y)

    'Select Right Clicked Row if its not the header row
    If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex > -1 Then
        'Clear any currently sellected rows
        DgvDelays.ClearSelection()
        Me.DgvDelays.Rows(e.RowIndex).Selected = True
        CmsRightClick.Show(DgvDelays, New System.Drawing.Point(MouseX, MouseY))
    End If


End Sub
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900