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?
Any help will be appreciated.
You can show the ContextMenuStrip on the click event:
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:
Button have down arrow right side of it and you can set menu of it from designer:
With ShowMenuUnderCursor:
MenuButton class:
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.
Thought this might be useful to someone. Thanks Jaex
Expanding @Jaex answer a little bit to allow for a separator line, conditional drawing of the arrow if nothing is configured and a separate click event for the main button body and the menu arrow.
It should be noted that for better alignment you can set the
button.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
Here is my slight improvement
Infragistics has the WinDropDownButton: http://help.infragistics.com/Help/NetAdvantage/WinForms/2012.1/CLR2.0/html/WinDropDownButton_About_WinDropDownButton.html
So it certainly exists, however you may not be looking for a paid third-party control.
I was fiddling with this issue as well and found an extremely simple solution (albeit a little dirty-hacky): place a
ComboBox
under theButton
, such that it shows the dropdown arrow right next to the button.Then use
SelectedIndexChanged
of theComboBox
to change theButton
behaviour, or do what you want it to do immediately.