在正确位置单击按钮时如何显示上下文菜单条
我想单击一个按钮并让它在按钮正下方显示一个 ContextMenuStrip
。当我尝试 PointToScreen 以及顶部和左侧坐标时,它不断显示在屏幕的左侧。
有什么建议吗?
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.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我知道这是一个老问题,但我认为它可能对其他人有帮助。以下代码将在单击的按钮下方显示上下文菜单,并且该按钮看起来像一个下拉按钮。
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.
我想通了:
I figured it out:
按钮下的 ContexMenuName,与按钮右侧对齐(扩展到按钮下方和左侧):
<代码>
ContexMenuName.Show(ButtonName, new Point(ButtonName.Width - ContexMenuName.Width, ButtonName.Height));
希望这会对某人有所帮助:)
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 :)
据我所知,您需要的代码在这里:
// 在按钮的右侧
在按钮的底部
在按钮的右下角
As far as I know the code you require was here:
// On the right of the button
At the bottom of the button
At the bottom right of the button
似乎已经有了正确的答案,但我认为这可能更漂亮:
There seems to be a right answer already, but I thought this might be a bit prettier:
确保在定位上下文菜单时向其传递正确的屏幕坐标。您需要使用 Control.PointToScreen 之类的东西,使用基于控件在其父级中的位置的 x、y 坐标。
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.
我有toolstripDropDown,单击toolstripDropDown 按钮后我想显示上下文菜单。
因此,根据上面的评论,我修改了 toolStripDropDown_Openining 事件中的代码,如下所示。
效果很好。
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.
在按钮正下方显示 MenuStrip
To show MenuStrip right under the button
以下解决方案适用于大多数控制
The following solution can be applied for most control
简单的方法
Easy way