2011-09-13 7 views
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)); 
    } 
} 
+1

あなたのプラットフォームがたくさん役立つだろう。 (silverlight、wpf、asp.net、winformsなど) –

答えて

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

あなたが値のためe.Xとe.Yを使用しているので、それが表示されていない理由があります。それらは画面上の実際の場所ではありません。それらは、DataGrid内のマウスの位置です。つまり、最初の行の最初のセルをクリックすると、そのコンポーネントの左上隅に表示されます。 e.Xおよびe.Yは、コンポーネント内のマウスの位置です。

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); 
    ... 
    ... 
関連する問題