c# - 无法设置上下文菜单条的位置?

标签 c# winforms contextmenustrip

我试图在我右击鼠标的地方打开一个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/

相关文章:

c# - 如何从 Web API 中的默认格式化程序构建自定义格式化程序?

c# - datagridview 中行分隔符的颜色

c# - 如何重新排列上下文菜单条中的项目?

c# - 通过 toolstripmenuitem 事件处理程序获取控件(treeView)

c# - 如何在 C# 中可靠地检查空值

c# - 如何计算矩阵行列式? n*n 或者只是 5*5

c# - 如何更改datagridview中的mysql查询?

c# - 如何在不按住 Alt 的情况下显示下划线(快捷方式)?

c# - 使用内存中查询实现自定义 QueryProvider

c# - 对齐按钮背景图片