【问题标题】:Add a Menu on the Button in c#在 C# 中的按钮上添加菜单
【发布时间】:2015-03-26 16:13:08
【问题描述】:

我想创建一个按钮,单击该按钮将显示一个菜单。我希望它看起来像这样(侧面有一个箭头):

我在 onClick 事件上尝试了以下代码:

Button btnSender = (Button)sender;
Point ptLowerLeft = new Point(0, btnSender.Height);
ptLowerLeft = btnSender.PointToScreen(ptLowerLeft);
contextMenuStrip1.Show(ptLowerLeft);

但输出是这样的:

如何创建一个右侧带有箭头的按钮,单击时菜单会显示在它旁边?

【问题讨论】:

  • 我猜你说的是Windows Forms吧?

标签: c# button contextmenustrip


【解决方案1】:

非常简短的答案是,我认为您将宽度与高度混淆了。换线就行了

Point ptLowerLeft = new Point(0, btnSender.Height);

Point ptUpperRight = new Point(btnSender.Width, 0);

但是,通常还有更多工作要做。特别是,根据按钮的位置,上下文菜单可能会显示在屏幕的可见区域之外。我认为最简单的解决方案是使用工具栏控件。据我记得有一个控件支持这种行为。

【讨论】:

  • 非常感谢乔治。 :)
【解决方案2】:

要添加到 Georg 的答案中:要添加图片,您只需编写:

button1.Image = Image.FromFile(yourImageFile); // or a resource etc..
button1.ImageAlign = ContentAlignment.MiddleRight;
button1.TextAlign = ContentAlignment.MiddleLeft;

【讨论】:

    【解决方案3】:

    你为什么不使用 menu 而不是 button 或者你可以添加一个带有小箭头的背景图片,你可以在悬停或点击按钮时显示它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多