Click here to Skip to main content
15,867,594 members
Articles / Multimedia / GDI+

A Basic Chooser-Style DropDown ToolBar Button

Rate me:
Please Sign up or sign in to vote.
1.67/5 (12 votes)
12 May 20052 min read 79.1K   460   36   3
This article shows how to implement a basic, chooser-style dropdown ToolBar button.

Screen Shot

Introduction

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:

Visual Studio Dropdown Button

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:

Property Page

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.

C#
private void On_toolBar_MouseDown(object sender, 
                    System.Windows.Forms.MouseEventArgs e)
{
    // Do nothing if there is no context menu
    if (this.m_contextMenu == null)
    {
        return;
    }

    // Ensure we clicked on the toolbar button
    if (!this.w_toolBarButton.Rectangle.Contains(new 
                     System.Drawing.Point(e.X,e.Y)))
    {
        return;
    }

    // Bring up the context menu
    if (!this.w_toolBarButton.Pushed)
    {
        this.w_toolBarButton.Pushed = true;
        this.ToolBarContextMenu.Show(this.w_toolBar, 
                    new System.Drawing.Point(0,22));
        this.Invalidate();
    }
}

private void On_ToolbarDropDownButton_Paint(object sender, 
                    System.Windows.Forms.PaintEventArgs e)
{
    this.w_toolBarButton.Pushed = false;
}

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionError Pin
Beneck28-Oct-05 14:05
Beneck28-Oct-05 14:05 
hello,
I don't know what is wrong but every time when I try to launch the app I have:
Unhandled Exception
type: System.Reflection.TargetInvocationException


in the line:
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));

I just loaded your project, all the files from zip are in the firectory. Confused | :confused:

can you help me?

regards
GeneralThats easy... Pin
rocket8120-Oct-05 2:50
rocket8120-Oct-05 2:50 
GeneralDropDownMenu in ToolbarButtons Pin
Maximilian Seifert10-Oct-05 3:06
Maximilian Seifert10-Oct-05 3:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.