Click here to Skip to main content
12,689,947 members (24,650 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


16 bookmarked

ButtonDropDown using Custom Control (ButtonDropDownMenu Control)

, 2 Jun 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
How to create a custom control of button which on click drops down a list of toolbar menu


This post helps to create a custom control of button which on click drops down a list of toolbar menu.


Image 1

In my earlier post regarding DropDownButton, SimpleButtonDropdownCtrl is explained in the simplest way. Now that one is upgraded by using it as a CustomControl.

This link helps to get a basic awareness of creating a custom control for those who are new to custom control.

Just add the attached CustomControl files ‘ButtonDropDownMenuControl’ to your project and it can be found in the Toolbox (check Image 2).


Image 2

Now drag this control to the form and add ToolbarMenuItems into it from the property window (Check image 3).

The arrow in the button is an image with middle-right aligned.


Image 3

For this custom control, an extra even handler is added to capture the clicked toolbarmenu. This can be found in the event handler list of property window (check image 4).


Image 4

The form where the control is used:

private void buttonDropDownMenuControl1_MenuButtonClick(object sender, EventArgs e)
    MessageBox.Show("You have clicked '" + 
	buttonDropDownMenuControl1.ClickedToolStripMenuItem.Name + "'");

Custom Control Code

public partial class ButtonDropDownMenuControl : Button
    private bool _menuClicked = false;
    private bool _clickEventRegistered = false;
    private ContextMenuStrip _contextMenuStripList;
    private List<ToolStripMenuItem> _menuItems = new List<ToolStripMenuItem>();
    private ToolStripMenuItem _clickedToolStripMenuItem = null;

    public ButtonDropDownMenuControl()

    protected override void OnPaint(PaintEventArgs pe)

    /// <summary>
    /// Declare New Even for MenuClick
    /// </summary>
    [Description("Occurs when the a menu item is clicked.")]
    public event EventHandler MenuButtonClick;

    [Description("List of Menu Items")]
    public List<ToolStripMenuItem> MenuItems
        get { return _menuItems; }
        set { _menuItems = new List<ToolStripMenuItem>(value); }

    protected override void OnClick(EventArgs e)
        //If the click event is not from menuItem
        if (!_menuClicked)
            //Initialize ContextMenuStrip
            if (_contextMenuStripList == null)
                _contextMenuStripList = new ContextMenuStrip();

            //Skip, if MenuItem already registered
            if (!_clickEventRegistered)
                foreach (ToolStripMenuItem mItem in this.MenuItems)
                    mItem.Click += new System.EventHandler
                _clickEventRegistered = true;

            //Pop-up ContextmenuStrip
            this.ContextMenuStrip = _contextMenuStripList;

            this.ContextMenuStrip.Show(this, new System.Drawing.Point(0, this.Height));
        else if (MenuButtonClick != null)
            MenuButtonClick(this, e);
        _menuClicked = false;

    private void AutoFillToolStripMenuItem_Click(object sender, EventArgs e)
        _menuClicked = true;
        _clickedToolStripMenuItem = (ToolStripMenuItem)sender;
        OnClick(new EventArgs());

    public ToolStripMenuItem ClickedToolStripMenuItem
        get { return _clickedToolStripMenuItem; }


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Balu Sathish
Software Developer UVJ Technologies
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Omar Gamil3-Jun-11 1:22
memberOmar Gamil3-Jun-11 1:22 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170113.4 | Last Updated 2 Jun 2011
Article Copyright 2011 by Balu Sathish
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid