vb.net ContextMenuStrip 未显示附近的光标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15313734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ContextMenuStrip not showing nearby cursor
提问by Robin L
I'm trying to create an event that shows a contextmenu when I right click a row in my datgridview.
当我右键单击 datgridview 中的一行时,我正在尝试创建一个显示上下文菜单的事件。
Here is an image of the problem that is happening:
这是正在发生的问题的图像:


And here is the code I am currently using:
这是我目前使用的代码:
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
P.S the screen capture doesn't show my cursor >.> but it's definitely not synced with the context menu!
PS 屏幕截图没有显示我的光标 >.> 但它绝对没有与上下文菜单同步!
EDIT: Ok guys I've solved it,
编辑:好的,我已经解决了,
I simply replaced Control.MousePosition to MousePosition and it worked!
我只是将 Control.MousePosition 替换为 MousePosition 并且它起作用了!
回答by user3115691
Neither of these worked for me. The solution that got the menu to pop up under the mouse was:
这些都不适合我。在鼠标下弹出菜单的解决方案是:
ContextMenuStrip1.Show(MousePosition.X, MousePosition.Y)
回答by Hans Passant
Mouse.Position is in screen coordinates. You'll need to provide the relative coordinates, relative from dgvStudents. They are handed to you on a silver platter through the event argument:
Mouse.Position 在屏幕坐标中。您需要提供相对坐标,相对于 dgvStudents。它们通过事件参数在银盘上交给你:
ContextMenuStrip1.Show(dgvStudents, e.Location)
Context menus are normally displayed in response to mouse-up so do favor the CellMouseUp event instead.
上下文菜单通常会响应鼠标抬起而显示,因此请改为使用 CellMouseUp 事件。

