9

我试图contextmenustrip在我右键单击鼠标的地方打开一个,但它总是显示在屏幕的左上角。

这是我使用的代码:

private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.Button == MouseButtons.Right)
    {
        contextMenuStrip1.Show(new Point(e.X,e.Y));
        doss.getdossier(connection.conn, Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value));
    }
}
4

2 回答 2

11
if (e.Button == MouseButtons.Right)
{
    contextMenuStrip1.Show(Cursor.Position);
}

它没有出现的原因是因为您使用 eX 和 eY 作为值。它们不是屏幕上的实际位置。它们是鼠标在数据网格中的位置。假设您单击了第一行的第一个单元格,它将位于该组件的左上角附近。eX 和 eY 是组件内的鼠标位置。

于 2011-09-13T15:30:29.423 回答
2

假设您在 Windows 窗体中,请尝试以下操作:

if (e.Button == MouseButtons.Right)
{
  Control control = (Control) sender;

  // Calculate the startPoint by using the PointToScreen 
  // method.

  var startPoint = control.PointToScreen(new Point(e.X, e.Y));
  contextMenuStrip1.Show(startPoint);
  ...
  ...
于 2011-09-13T15:32:28.327 回答