The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
This article shows how to implement a basic, chooser-style dropdown toolbar button. It seemed very strange to me that this simple widget was nowhere to be found in Visual Studio .NET, given that it exists in almost every single Windows application.
The closest you can get is a dropdown button that looks like this:
This is fine if the purpose of the button is two-fold (to perform some action in addition to presenting a choice), however sometimes you simply want to present a choice to the user. A good example is implementing a view configuration chooser button, such as can be found in the Windows file explorer. Here, allowing the user to click on the button makes no sense at all.
Using the code
This widget is implemented as a UserControl, which can be included in any C# Windows project. To use the widget, simply drag and drop it onto your form, and place it on top of an existing, docked toolbar. You will need both a
ContextMenu, and an
ImageList on your form, which can be associated with the widget in its property sheet:
The code to implement the button is quite simple - although it took a while to get it right... The key is to use the
MouseDown event to bring up the context menu, and to use the
Paint event of the control to set the '
Pushed' state of the button to '
false', when the menu goes away. In order to force the
Paint event to fire, you have to call
Invalidate() on the control before bringing up the context menu.
private void On_toolBar_MouseDown(object sender,
if (this.m_contextMenu == null)
this.w_toolBarButton.Pushed = true;
private void On_ToolbarDropDownButton_Paint(object sender,
this.w_toolBarButton.Pushed = false;