Introduction
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()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
[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 (!_menuClicked)
{
if (_contextMenuStripList == null)
_contextMenuStripList = new ContextMenuStrip();
if (!_clickEventRegistered)
{
foreach (ToolStripMenuItem mItem in this.MenuItems)
{
_contextMenuStripList.Items.Add(mItem);
mItem.Click += new System.EventHandler
(this.AutoFillToolStripMenuItem_Click);
}
_clickEventRegistered = true;
}
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 member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.