Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

ButtonDropDown using Custom Control (ButtonDropDownMenu Control)

, 2 Jun 2011
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

Introduction

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

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

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.

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

Image_4.png

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()
    {
        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)

Share

About the Author

Balu Sathish
Software Developer Calpine Technologies
India India
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberOmar Gamil3-Jun-11 0:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.8.140922.1 | Last Updated 2 Jun 2011
Article Copyright 2011 by Balu Sathish
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid