【问题标题】:C# DataGridView opening ContextMenu at location of Right ClickC# DataGridView 在右键单击的位置打开 ContextMenu
【发布时间】: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.MousePosition
  • Cursor.Position

可能还有其他一些。

编辑 2

这就是我所说的偏移量——一些解决方案会导致这个偏移量以一定的幅度变化(一些真的很远等)——但所有的偏移量都与实际光标的偏移量一样。

编辑 3

我的contextCellnew ContextMenu()

【问题讨论】:

  • 偏移量是什么意思?由于上下文中没有出现光标所在的位置?
  • 我已经编辑了一个截图 :) 但是是的 - 与光标的偏移是
  • 如果使用 ContextMenuStrip,请尝试 contextCell.Show(Cursor.Position);
  • 感谢@LarsTech,但也尝试过(更新了我的 OP),但它实际上使情况变得更糟——大约是上面屏幕截图中偏移量的两倍 :(
  • 改用 ContextMenuStrip。

标签: c# winforms datagridview contextmenu


【解决方案1】:

选项 1: 为行显示上下文菜单的最简单解决方案是将上下文菜单分配给 DataGridView 的 RowTemplate.ContextMenuStrip 属性:

dataGridView1.RowTemplate.ContextMenuStrip = contextMenuStrip1;

选项2:另外,如果你想在显示ContextMenuStrip之前选择单元格,处理CellContextMenuStripNeeded事件就足够了:

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;
    }
}

你的错误是什么?

您以错误的方式计算DataGridView 上的鼠标位置。您使用的是PointToClient,即this.PointToClient,而您需要使用DataGridView的方法,例如dataGridView1.PointToClient

myContextMenu.Show(dataGridView1,dataGridView1.PointToClient(Cursor.Position));

仅供参考,您只需使用此代码显示ContextMenu,无需使用ContextMenuStrip

但我强烈建议你使用ContextMenuStrip

【讨论】:

    【解决方案2】:

    this.dataGridView1.ContextMenuStrip = this.contextMenuStrip1;
    

    并处理DataGridView.CellContextMenuStripNeeded Event

    【讨论】:

      【解决方案3】:

      答案是让contextCell 变成ContextMenuStrip,而不是原来的ContextMenu

      毕竟…………

      感谢您的回复。

      【讨论】:

      • 我描述了你的错误。在答案中阅读它:)
      • 是的,很酷,谢谢——PointToClient 方法只是我尝试过的方法之一——很确定我能用的每一种方法都可以大声笑——但答案只是让它成为 ContextMenuStrip 而不是 ContextMenu 和将 ContextMenuStrip 附加到 DataGridView,然后我什至不需要设置它的位置——它马上就可以正常工作了 :)
      • 您似乎没有理解我在答案中所说的内容。您还可以显示ContextMenu。但是你应该使用正确的位置。你的错误只是通过了不正确的位置。显然,您也可以显示ContextMenu,并且不必使用ContextMenuStrip
      • 啊,是的,我明白了——抱歉没有通读一遍——我明白你关于引用 dataGridView 的意思——我没有尝试过。一切都好——问题解决了! :D
      猜你喜欢
      • 2013-06-18
      • 2011-06-16
      • 2014-02-09
      • 2011-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-01
      • 2012-07-20
      相关资源
      最近更新 更多