C# 在正确位置单击按钮时如何显示上下文菜单条
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1549823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to show contextmenustrip when a button is clicked in the right position
提问by leora
I want to click on a button and have it show a ContextMenuStrip
right below the button. It keeps showing up in the left hand side of the screen when i try PointToScreen
and top and left coordinates.
我想单击一个按钮并让它ContextMenuStrip
在按钮下方显示一个。当我尝试PointToScreen
顶部和左侧坐标时,它一直显示在屏幕的左侧。
Any suggestions?
有什么建议?
采纳答案by leora
I figured it out:
我想到了:
layoutMenus.Show(Cursor.Position.X, Cursor.Position.Y);
回答by Michael Todd
Make sure that when you're positioning the context menu that you pass it the correct screen coordinates. You'll need to use something like Control.PointToScreen, using the x, y, coordinates based on the position of the control in its parent.
确保在定位上下文菜单时向它传递正确的屏幕坐标。您需要使用 Control.PointToScreen 之类的东西,使用基于控件在其父控件中的位置的 x、y 坐标。
回答by A J Qarshi
I know this is an old question but I think it may help other people. Following code will display the context menu just below the button being clicked and the button will look like a dropdown button.
我知道这是一个老问题,但我认为它可以帮助其他人。以下代码将在被单击的按钮下方显示上下文菜单,该按钮看起来像一个下拉按钮。
private void Button1_Click(object sender, EventArgs e)
{
Button btnSender = (Button)sender;
Point ptLowerLeft = new Point(0, btnSender.Height);
ptLowerLeft = btnSender.PointToScreen(ptLowerLeft);
ctMenuStrip.Show(ptLowerLeft);
}
回答by qursaan
Easy way
简单的方法
contextMenuStrip1.Show(Button1, Button1.PointToClient(Cursor.Position));
contextMenuStrip1.Show(Button1, Button1.PointToClient(Cursor.Position));
回答by lllllllllllllIllllIll
As far as I know the code you require was here:
据我所知,您需要的代码在这里:
// On the right of the button
// 在按钮的右边
ContextMenuName.Show(ButtonName.Left + ButtonName.Width + this.Left, ButtonName.Top + this.Top);
At the bottom of the button
在按钮底部
ContextMenuName.Show(ButtonName.Left + this.Left, ButtonName.Top + ButtonName.Height + this.Top);
At the bottom right of the button
在按钮的右下角
ContextMenuName.Show(ButtonName.Left + ButtonName.Width + this.Left, ButtonName.Top + ButtonName.Height + this.Top);
回答by Sagar
I am having toolstripDropDown and after clicking on toolstripDropDown button i wanted to show the context menu. So from above comments i modified my code in toolStripDropDown_Openining event as follows. it works fine.
我有 toolstripDropDown,在单击 toolstripDropDown 按钮后,我想显示上下文菜单。所以根据上面的评论,我在 toolStripDropDown_Openining 事件中修改了我的代码,如下所示。它工作正常。
void toolStripDropDownButton_DropDownOpening(object sender, EventArgs e)
{
ToolStripDropDownButton btnSender = (ToolStripDropDownButton)sender;
Point ptLowerRight = new Point(btnSender.Bounds.Right, btnSender.Bounds.Bottom);
ptLowerRight = PointToScreen(ptLowerRight);
contextMenuStrip.Show(ptLowerRight);
}
回答by saper_2
ContexMenuName under button, aligned to the right side of button (expands to below button and to the left):
ContexMenuName.Show(ButtonName, new Point(ButtonName.Width - ContexMenuName.Width, ButtonName.Height));
Hope this will help sb :)
按钮下的 ContexMenuName,与按钮右侧对齐(扩展到按钮下方和左侧):
ContexMenuName.Show(ButtonName, new Point(ButtonName.Width - ContexMenuName.Width, ButtonName.Height));
希望这对某人有所帮助 :)
回答by Osman ?elik
contextMenuStrip1.Show(button1.PointToScreen(new Point(0, button1.Height)));
To show MenuStrip right under the button
在按钮正下方显示 MenuStrip