Click here to Skip to main content
15,937,127 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to locate the position of a context menu strip on mouse click
Posted
Updated 13-Apr-20 22:37pm
Comments
Kenneth Haugland 10-Jul-12 9:07am    
Do this problem about windows forms or WPF?

This should do the trick...
Me.ContextMenuStrip1.Bounds.Location


VB
Private Sub Form1_MouseDoubleClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDoubleClick
        Me.ContextMenuStrip1.Show(MousePosition)
    End Sub

    Private Sub TestToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles TestToolStripMenuItem.Click
        MessageBox.Show(Me.ContextMenuStrip1.Bounds.Location.X & "x" & Me.ContextMenuStrip1.Bounds.Location.Y & _
                        vbCrLf & MousePosition.X & "x" & MousePosition.Y)
 
Share this answer
 
It is normally shown relative to a particular control - it's just a case of offsetting it (if you want to) by an amount from the top left hand corner of the relevant control by manually constructing it and using the Show method: MSDN has an example: http://msdn.microsoft.com/en-us/library/s00cc2f3.aspx[^]
 
Share this answer
 
In DataGridView

VB
Private Sub YourDataGrid_CellMouseDown(sender As Object, e As DataGridViewCellMouseEventArgs) Handles YourDataGrid.CellMouseDown
        If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex >= 0 Then
            YourDataGrid.Rows(e.RowIndex).Selected = True
            'if possible insert code here to read selected option from context menu and get relevant content from respective cell

            YourContextMenu.Show(MousePosition.X, MousePosition.Y)
        End If
    End Sub
 
Share this answer
 
v2
Comments
CHill60 2-Apr-17 7:43am    
The question was answered over 4 years ago
VB
Private Sub DataGridView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
    If e.Button = Windows.Forms.MouseButtons.Right Then
        ContextMenuStrip1.Show(CType(sender, Control), e.Location)
    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