Introduction
This post helps to create a ButtonDropDown
in Windows Form (I was not able to find a control in Visual Studio for my requirement, so I made one and I’m not sure whether there is one already). For this, I’m using a combination of Button
control and ContextMenuStrip
control.
The arrow in the button is an image with middle-right aligned.
- Initialize a
ContextMenuStrip
with the required MenuItems
- Register the click events of
ContextMenuStripMenuItems
- Pop out the context menu on Button click with the given x, y axis (w.r.t to the button), y axis value should be the height of the button:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ButtonDropDownTest
{
public partial class Form1 : Form
{
private ContextMenuStrip _contextMenuAutoFill;
public Form1()
{
InitializeComponent();
CreateContextMenuStrip();
}
private void CreateContextMenuStrip()
{
_contextMenuAutoFill = new ContextMenuStrip();
_contextMenuAutoFill.Items.Add("Dropdown item 1");
_contextMenuAutoFill.Items.Add("Dropdown item 2");
_contextMenuAutoFill.Items.Add("Dropdown item 3");
_contextMenuAutoFill.Items.Add("Dropdown item 4");
foreach (ToolStripMenuItem mItem in _contextMenuAutoFill.Items)
mItem.Click +=
new System.EventHandler(this.AutoFillToolStripMenuItem_Click);
}
private void button1_Click(object sender, EventArgs e)
{
button1.ContextMenuStrip = _contextMenuAutoFill;
button1.ContextMenuStrip.Show
(button1, new System.Drawing.Point(0, button1.Height));
}
private void AutoFillToolStripMenuItem_Click(object sender, EventArgs e)
{
string m = ((ToolStripMenuItem)sender).Text;
MessageBox.Show("You have clicked '" + m + "'");
}
}
}
This link explains how to create this control using custom control.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.