我试图在我右击鼠标的地方打开一个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));
}
}
最佳答案
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(Cursor.Position);
}
它没有出现的原因是因为您使用 e.X 和 e.Y 作为值。它们不是屏幕上的实际位置。它们是鼠标在数据网格中的位置。假设您单击了第一行的第一个单元格,该单元格将位于该组件的左上角附近。 e.X 和 e.Y 是组件内的鼠标位置。
关于c# - 无法设置上下文菜单条的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7404401/