我正在编写代码编辑器,我正在制作一个mouseevent,专门用于右键单击此代码:
if (e.Button == MouseButtons.Right)
{
lbright.Show(Cursor.Position.X, Cursor.Position.Y);
lbright.BringToFront();
}
但问题是我每次运行它都会出现“方法重载”错误并指向.Show(Cursor.Position.X, Cursor.Position.Y);
我该如何避免它?任何帮助都将非常感谢,谢谢!
答案 0 :(得分:3)
您正在尝试显示上下文菜单。使用专为该任务设计的类:ContextMenu
。
此课程有一个Show
方法,可让您定位菜单:Show(Control, Point)
答案 1 :(得分:2)
错误很明显:Control.Show()
方法只有一个重载:一个没有参数。
答案 2 :(得分:1)
如果您使用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));
}
答案 3 :(得分:0)
根据我的问题,我认为这可能会有所帮助
lbright.Top=Cursor.Position.X;
lbright.Left=Cursor.Position.Y;
lbright.Show();