在正确位置单击按钮时如何显示上下文菜单条

发布于 2024-08-07 14:12:13 字数 118 浏览 11 评论 0原文

我想单击一个按钮并让它在按钮正下方显示一个 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 技术交流群。

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

发布评论

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

评论(10

朱染 2024-08-14 14:12:13

我知道这是一个老问题,但我认为它可能对其他人有帮助。以下代码将在单击的按钮下方显示上下文菜单,并且该按钮看起来像一个下拉按钮。

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

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);
}
强辩 2024-08-14 14:12:13

我想通了:

layoutMenus.Show(Cursor.Position.X, Cursor.Position.Y);

I figured it out:

layoutMenus.Show(Cursor.Position.X, Cursor.Position.Y);
风筝有风,海豚有海 2024-08-14 14:12:13

按钮下的 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 :)

神魇的王 2024-08-14 14:12:13

据我所知,您需要的代码在这里:

// 在按钮的右侧

ContextMenuName.Show(ButtonName.Left + ButtonName.Width + this.Left, ButtonName.Top + this.Top);

在按钮的底部

ContextMenuName.Show(ButtonName.Left + this.Left, ButtonName.Top + ButtonName.Height + this.Top);

在按钮的右下角

ContextMenuName.Show(ButtonName.Left + ButtonName.Width + this.Left, ButtonName.Top + ButtonName.Height + this.Top);

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);
祁梦 2024-08-14 14:12:13

似乎已经有了正确的答案,但我认为这可能更漂亮:

private void button_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    menuStrip.Show(btn, new Point(0, 0)); // offset from the edge of your button
}

There seems to be a right answer already, but I thought this might be a bit prettier:

private void button_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    menuStrip.Show(btn, new Point(0, 0)); // offset from the edge of your button
}
贪了杯 2024-08-14 14:12:13

确保在定位上下文菜单时向其传递正确的屏幕坐标。您需要使用 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.

顾北清歌寒 2024-08-14 14:12:13

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

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.

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);
    }
如歌彻婉言 2024-08-14 14:12:13
 contextMenuStrip1.Show(button1.PointToScreen(new Point(0, button1.Height)));

在按钮正下方显示 MenuStrip

 contextMenuStrip1.Show(button1.PointToScreen(new Point(0, button1.Height)));

To show MenuStrip right under the button

放飞的风筝 2024-08-14 14:12:13

以下解决方案适用于大多数控制

contextMenuStrip1.Show(sender,sender.Location)

The following solution can be applied for most control

contextMenuStrip1.Show(sender,sender.Location)
煞人兵器 2024-08-14 14:12:13

简单的方法

contextMenuStrip1.Show(Button1,
Button1.PointToClient(Cursor.Position));

Easy way

contextMenuStrip1.Show(Button1,
Button1.PointToClient(Cursor.Position));

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