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

发布于 2024-12-04 06:30:52 字数 423 浏览 5 评论 0原文

我试图在右键单击鼠标的位置打开一个 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));
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悸初 2024-12-11 06:30:52
if (e.Button == MouseButtons.Right)
{
    contextMenuStrip1.Show(Cursor.Position);
}

它没有出现的原因是您使用 eX 和 eY 作为值。它们不是屏幕上的实际位置。它们是鼠标在数据网格中的位置。假设您单击了第一行的第一个单元格,该单元格将位于该组件的左上角附近。 eX 和 eY 是组件内的鼠标位置。

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.

潜移默化 2024-12-11 06:30:52

假设您使用的是 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);
  ...
  ...

assuming you are in Windows Forms, try this:

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);
  ...
  ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文