【发布时间】:2016-07-21 17:15:07
【问题描述】:
我已经环顾了很长时间,试图找到一个可行的解决方案,但我想问一个问题:
我的应用程序的对话框表单中有一个 DataGridView,我希望在右键单击单元格时显示一个 ContextMenu。
我有右键单击并且 ContextMenu 看起来很好,但是无论我尝试 StackExchange 上的哪种解决方案,它总是偏移很多。
这与表单和/或其父级有关吗?还是我只是在这里愚蠢地遗漏了什么?
谢谢 杰米
Form.cs
private void dataGridContents_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
Debug.WriteLine("Cell right clicked!");
DataGridViewCell cell = (sender as DataGridView)[e.ColumnIndex, e.RowIndex];
contextCell.Show(cell.DataGridView, PointToClient(Cursor.Position));
if (!cell.Selected)
{
cell.DataGridView.ClearSelection();
cell.DataGridView.CurrentCell = cell;
cell.Selected = true;
}
}
}
}
编辑
对不起,我试过了:
new Point(e.X, e.Y)new Point(e.Location.X, e.Location.Y)new Point(MousePosition.X, MousePosition.Y)PointToClient(e.X, e.Y)new Point(Cursor.Position.X, Cursor.Position.Y)Control.MousePositionCursor.Position
可能还有其他一些。
编辑 2
这就是我所说的偏移量——一些解决方案会导致这个偏移量以一定的幅度变化(一些真的很远等)——但所有的偏移量都与实际光标的偏移量一样。
编辑 3
我的contextCell 是new ContextMenu()
【问题讨论】:
-
偏移量是什么意思?由于上下文中没有出现光标所在的位置?
-
我已经编辑了一个截图 :) 但是是的 - 与光标的偏移是
-
如果使用 ContextMenuStrip,请尝试
contextCell.Show(Cursor.Position); -
感谢@LarsTech,但也尝试过(更新了我的 OP),但它实际上使情况变得更糟——大约是上面屏幕截图中偏移量的两倍 :(
-
改用 ContextMenuStrip。
标签: c# winforms datagridview contextmenu