带有下拉菜单的 Windows.Forms 按钮 [英] Windows.Forms button with drop-down menu

查看:24
本文介绍了带有下拉菜单的 Windows.Forms 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 .NET 上使用 Windows.Forms 开发简单的 C# 应用程序.我需要一些按钮来显示带有子类别的下拉菜单 - 很像 ToolStripMenu,但是按钮,你知道.我搜索了它,但找不到任何变体.

I'm developing simple C# application using Windows.Forms on .NET. I need some button that will show a drop-down menu with subcategories - much like ToolStripMenu, but the button, you know. I searched for it and could not found any variants.

我的问题是:有没有办法做到这一点,也许是一些允许附加菜单的秘密按钮属性?

My question is: is there any way to do this, maybe some secret button property that allows attach menu to it?

任何帮助将不胜感激.

推荐答案

可以在点击事件上显示ContextMenuStrip:

You can show the ContextMenuStrip on the click event:

private void button1_Click(object sender, EventArgs e) {
  contextMenuStrip1.Show(button1, new Point(0, button1.Height));
}

要自行决定是在按钮上方还是下方显示菜单,您可以尝试使用以下代码,该代码测量菜单并确定它是否部分离屏:

To make your own determination whether to show the menu above or below the button, you can try using this code, which measures the menu and determines whether or not it would be partially offscreen:

private void button1_Click(object sender, EventArgs e) {
  Point screenPoint = button1.PointToScreen(new Point(button1.Left, button1.Bottom));
  if (screenPoint.Y + contextMenuStrip1.Size.Height > Screen.PrimaryScreen.WorkingArea.Height) {
    contextMenuStrip1.Show(button1, new Point(0, -contextMenuStrip1.Size.Height));
  } else {
    contextMenuStrip1.Show(button1, new Point(0, button1.Height));
  }    
}

这篇关于带有下拉菜单的 Windows.Forms 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆