现在重载方法'Show'接受2个参数

本文关键字:接受 2个 参数 Show 重载 方法 | 更新日期: 2023-09-27 18:13:19

我正在编写代码编辑器,我为右键单击创建了一个mouseevent,具体如下代码:

if (e.Button == MouseButtons.Right)
{               
      lbright.Show(Cursor.Position.X, Cursor.Position.Y);
      lbright.BringToFront();               
}

但问题是每次我运行它'重载方法'错误出现,并指向.Show(Cursor.Position.X, Cursor.Position.Y);

我怎样才能避免呢?

现在重载方法'Show'接受2个参数

您正在尝试显示上下文菜单。使用为该任务设计的类:ContextMenu

这个类有一个Show方法可以让你定位菜单:Show(Control, Point)

这个错误很明显:Control.Show()方法只有一个重载:一个没有参数的重载。

如果你想移动控件,设置TopLeft属性

如果你使用一个ContextMenu并使用。show (Control, Point)的重载和一个偏移量,你应该得到你的菜单显示:

if (e.Button == MouseButtons.Right)
{               
    // build a new ContextMenu with some menu items, let's use copy and paste
    ContextMenu ctxRightClick = new ContextMenu(new MenuItem[]
    {
        new MenuItem("Copy"),
        new MenuItem("Paste")
    });
    // as per the documentation, the Point used by .Show is relative to the control you pass in so we calculate an offset from the mouse position
    int xOffset = Cursor.Position.X - myForm.Location.X;
    int yOffset = Cursor.Position.Y - myForm.Location.Y;
    // now show the context menu as a child of myForm at the specified offset
    ctxRightClick.Show(myForm, new Point(xOffset, yOffset));
}

从你的问题中,我认为这可能有帮助

lbright.Top=Cursor.Position.X;
lbright.Left=Cursor.Position.Y;
lbright.Show();