Can't set the position of a contextmenustrip?

Tarik Mokafih picture Tarik Mokafih · Sep 13, 2011 · Viewed 9.1k times · Source

I'm trying to open a contextmenustrip at the place where I right-clicked the mouse, but it always shows at top left of the screen.

Here is the code I used:

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

Answer

Stuart Thomson picture Stuart Thomson · Sep 13, 2011
if (e.Button == MouseButtons.Right)
{
    contextMenuStrip1.Show(Cursor.Position);
}

the reason it's not appearing is because you are using e.X and e.Y for the values. They are not the actual location on the screen. They are the location of the mouse within the datagrid. So say you clicked on the first cell of the first row, that will be near the top left of that component. e.X and e.Y are the mouse locations within the component.