Windows.Forms button with drop-down menu

后端 未结 9 2089
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 14:31

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 b

9条回答
  •  遥遥无期
    2020-12-02 14:56

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

提交回复
热议问题