Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C#

ButtonDropDown using Custom Control (ButtonDropDownMenu Control)

Rate me:
Please Sign up or sign in to vote.
4.32/5 (6 votes)
2 Jun 2011CPOL 20.7K   1.1K   16   1
How to create a custom control of button which on click drops down a list of toolbar menu

Introduction

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

ButtonDropDownMenuControl/Image_1.png

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).

ButtonDropDownMenuControl/Image_2.png

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.

ButtonDropDownMenuControl/Image_3.png

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).

ButtonDropDownMenuControl/Image_4.png

Image 4

The form where the control is used:

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

Custom Control Code

C#
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()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
    }

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

    [Description("List of Menu Items")]
    [Category("ButtonDropDowm")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    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)
                {
                    _contextMenuStripList.Items.Add(mItem);
                    mItem.Click += new System.EventHandler
				(this.AutoFillToolStripMenuItem_Click);
                }
                _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; }
    }
}

License

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


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

Comments and Discussions

 
GeneralMy vote of 5 Pin
Omar Gameel Salem3-Jun-11 0:22
professionalOmar Gameel Salem3-Jun-11 0: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.