2013-03-09 61 views
2

我试图创建一个事件,显示一个contextmenu,当我右键单击我的datgridview中的一行。ContextMenuStrip不显示附近的光标

这是问题的一个形象,正在发生的事情:

enter image description here

这里是我目前正在使用的代码:

Private Sub dgvStudents_CellMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvStudents.CellMouseDown 
    Dim rowClicked As DataGridView.HitTestInfo = dgvStudents.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 
     dgvStudents.ClearSelection() 
     Me.dgvStudents.Rows(e.RowIndex).Selected = True 
     ContextMenuStrip1.Show(dgvStudents, Control.MousePosition) 

    End If 
End Sub 

PS截屏不显示我的cursor>。>但它绝对不会与上下文菜单同步!

编辑:好的家伙,我已经解决了它,

我简单地更换Control.MousePosition到MousePosition和它的工作!

+0

上次我使用上下文菜单时,我不需要在cellmousedown事件中显示它。你有没有尝试过将上下文菜单添加为datagridview的属性? – 2013-03-09 17:32:40

+0

是的,我试了这个队友,问题是它不会选择它的行和它的显示当你点击标题>> – 2013-03-09 17:34:26

+1

请发布你的解决方案作为答案。 – Kermit 2013-03-09 17:52:41

回答

4

Mouse.Position在屏幕坐标中。您需要提供相对于dgvStudents的相对坐标。他们通过事件参数交给你一个银盘:

ContextMenuStrip1.Show(dgvStudents, e.Location) 

上下文菜单通常显示在响应鼠标向上所以做有利于CellMouseUp事件来代替。

5

这些都不适合我。让菜单在鼠标下弹出的解决方案是:

ContextMenuStrip1.Show(MousePosition.X, MousePosition.Y) 
+0

这也适用于我,而其他人没有。 – LPChip 2018-02-26 15:37:51