ButtonDropDown using Custom Control (ButtonDropDownMenu Control)






4.32/5 (6 votes)
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.
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).
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.
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).
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; }
}
}