C#DataGridView在右键单击位置打开ContextMenu [英] C# DataGridView opening ContextMenu at location of Right Click
问题描述
我已经寻找了很长一段时间,现在正在寻找一个可行的解决方案,但是我想问一个问题:
我在对话框表单中有一个DataGridView在我的应用中,我希望ContextMenu出现在单元格的右键单击上。
我单击鼠标右键,然后ContextMenu可以正常显示,但是无论采用哪种解决方案在我尝试的StackExchange上,它总是偏移很多。
这与表单和/或父表单有关吗?还是我只是在这里愚蠢地丢失了一些东西?
感谢
Jamie
表格。 cs
private void dataGridContents_CellMouseDown(object sender,DataGridViewCellMouseEventArgs e)
{
if( e.Button == MouseButtons.Right)
{
if(e.RowIndex< -1&& e.ColumnIndex> -1)
{
调试。 WriteLine(单元格单击右键!);
DataGridViewCell单元格=(发送为DataGridView)[e.ColumnIndex,e.RowIndex];
contextCell.Show(cell.DataGridView,PointToClient(Cursor.Position));
if(!cell.Selected)
{
cell.DataGridView.ClearSelection();
cell.DataGridView.CurrentCell =单元格;
cell.Selected = true;
}
}
}
}
编辑
抱歉,我已经尝试过:
-
新点(eX,eY)
-
新点(e.Location.X,e.Location。 Y)
-
新点(MousePosition.X,MousePosition.Y)
-
PointToClient(eX,eY)
-
new Point(Cursor.Position.X, Cursor.Position.Y)
-
Control.MousePosition
-
Cursor.Position
以及其他一些
编辑2
这就是我所说的偏移量-一些解决方案会导致这种情况偏移量在一定程度上有所不同(有些距离很远等等)-但所有偏移量都与实际光标的偏移量一样。 i.stack.imgur.com/k0XfF.png rel = nofollow noreferrer>
编辑3
我的 contextCell
是新的ContextMenu()
选项1:对于行显示上下文菜单,最简单的解决方案是将上下文菜单分配给 RowTemplate.ContextMenuStrip
DataGridView的属性:
dataGridView1.RowTemplate.ContextMenuStrip = contextMenuStrip1;
选项2:另外,如果您想在显示 ContextMenuStrip
,足以处理 CellContextMenuStripNeeded
事件:
私有无效dataGridView1_CellContextMenuStripNeeded(对象发送者,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
if(e.RowIndex> -1&& e.ColumnIndex> -1)
{
dataGridView1.CurrentCell = dataGridView1.Rows [e.RowIndex] .Cells [e.ColumnIndex];
e.ContextMenuStrip = contextMenuStrip1;
}
}
您的错误是什么?
您正在计算鼠标在 DataGridView
中的位置错误的方式。您正在使用 PointToClient
,这意味着 this.PointToClient
,而您需要使用的方法DataGridView
,例如 dataGridView1.PointToClient
:
myContextMenu.Show(dataGridView1,dataGridView1.PointToClient(Cursor.Position));
仅提供您的信息,您只需显示 ContextMenu
使用此代码,则无需使用 ContextMenuStrip
。
但我强烈建议您使用 ContextMenuStrip
。
I've looked around for quite a while now trying to find a working solution but am resorting to asking a question:
I have a DataGridView in a Dialog Form in my app, that I want a ContextMenu to appear on Right-Click of Cell.
I have the right-click and the ContextMenu appearing fine, however no matter what solution on StackExchange I attempt, it is always offset quite alot.
Is this to do with the form and/or it's parent? Or am I just stupidly missing something here?
Thanks Jamie
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;
}
}
}
}
Edit
Sorry, I have tried:
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.MousePosition
Cursor.Position
and probably a few others.
Edit 2
This is what I mean by offset -- some of the solutions cause this offset to vary in certain magnitudes (some really far away etc) -- but all are offset like those from the actual Cursor.
Edit 3
My contextCell
is a new ContextMenu()
Option 1: The most simple solution for showing a context menu for rows is assigning the context menu to RowTemplate.ContextMenuStrip
property of DataGridView:
dataGridView1.RowTemplate.ContextMenuStrip = contextMenuStrip1;
Option 2: Also if you want to select the cell before showing the ContextMenuStrip
, it's enough to handle CellContextMenuStripNeeded
event:
private void dataGridView1_CellContextMenuStripNeeded(object sender,
DataGridViewCellContextMenuStripNeededEventArgs e)
{
if (e.RowIndex > -1 && e.ColumnIndex > -1)
{
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
e.ContextMenuStrip = contextMenuStrip1;
}
}
What was your mistake?
You are calculating the mouse position on the DataGridView
in a wrong way. You are using PointToClient
which means this.PointToClient
, while you need to use the method of DataGridView
, for example dataGridView1.PointToClient
:
myContextMenu.Show(dataGridView1,dataGridView1.PointToClient(Cursor.Position));
Just for your information you can simply show ContextMenu
using this code and there is no need to use ContextMenuStrip
.
But I strongly recommend you to use ContextMenuStrip
.
这篇关于C#DataGridView在右键单击位置打开ContextMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!