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 に答える