Windows.Forms button with drop-down menu

后端 未结 9 2083
没有蜡笔的小新
没有蜡笔的小新 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:59

    The simplest option would be to use the ToolStripDropDownButton in an undocked ToolStrip that only shows the single button. Then you can add sub-items to it, etc. To do this: - drag a Toolstrip onto your control/form - use the layout helper to add a DropDownButton - set GripStyle to Hidden - set Dock to None

    The result is a standalone toolbar-style button that supports the drop-down behavior that you described.

    0 讨论(0)
  • 2020-12-02 15:08

    Button have down arrow right side of it and you can set menu of it from designer:

    ss

    With ShowMenuUnderCursor:

    ss

    MenuButton class:

    public class MenuButton : Button
    {
        [DefaultValue(null)]
        public ContextMenuStrip Menu { get; set; }
    
        [DefaultValue(false)]
        public bool ShowMenuUnderCursor { get; set; }
    
        protected override void OnMouseDown(MouseEventArgs mevent)
        {
            base.OnMouseDown(mevent);
    
            if (Menu != null && mevent.Button == MouseButtons.Left)
            {
                Point menuLocation;
    
                if (ShowMenuUnderCursor)
                {
                    menuLocation = mevent.Location;
                }
                else
                {
                    menuLocation = new Point(0, Height);
                }
    
                Menu.Show(this, menuLocation);
            }
        }
    
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
    
            if (Menu != null)
            {
                int arrowX = ClientRectangle.Width - 14;
                int arrowY = ClientRectangle.Height / 2 - 1;
    
                Brush brush = Enabled ? SystemBrushes.ControlText : SystemBrushes.ControlDark;
                Point[] arrows = new Point[] { new Point(arrowX, arrowY), new Point(arrowX + 7, arrowY), new Point(arrowX + 3, arrowY + 4) };
                pevent.Graphics.FillPolygon(brush, arrows);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-02 15:08

    easy was we can do it. this may help :)

    ContextMenuStrip contextMenuStrip1 = new ContextMenuStrip();
    
            private void button1_Click(object sender, EventArgs e)
            {
                contextMenuStrip1.Items.Clear();
                contextMenuStrip1.Items.Add("item1");
                contextMenuStrip1.Items.Add("item2");
    
                contextMenuStrip1.Show(button1, new Point(0, button1.Height));
            }
    
            private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
                if (e.ClickedItem.Text == "item1")
                {
                    MessageBox.Show(e.ClickedItem.Text);
                }
            }
    
    0 讨论(0)
提交回复
热议问题