Windows.Forms button with drop-down menu

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

    Jaex's MenuButton class above was perfect for me. I did add the logic below into the OnMouseDown so that the context menu would only show up if I clicked on the arrow. The normal click event would get triggered if I clicked in the larger portion. Allowed for a "Default" click action.

    if (Menu != null && mevent.Button == MouseButtons.Left)
    {
        if (mevent.Location.X >= this.Width - 14)
        {
            System.Drawing.Point menuLocation;
    
            if (ShowMenuUnderCursor)
            {
                menuLocation = mevent.Location; 
            }
            else
            {
                menuLocation = new System.Drawing.Point(0, Height);
            }
    
            Menu.Show(this, menuLocation);
        }
    }
    

    Thought this might be useful to someone. Thanks Jaex

提交回复
热议问题