about toolStripDropDownButton

4.4k views Asked by At

I have a form. I've added the strip down button using drag and drop in the form. How can I (in the program) create and fill the toolStripMenu Item? My menu could contain different element...with different names.

2

There are 2 answers

5
digEmAll On

If you want to add items programmatically to a ToolStripDropDownButton just do:

var item1 = new ToolStripButton("my button");
toolStripDropDownButton1.DropDownItems.Add(item1);

var item2 = new ToolStripComboBox("my combo");
toolStripDropDownButton1.DropDownItems.Add(item2);

// etc ...

Instead, if you need to add other ToolStripDropDownButton or other elements directly to you menu (ToolStrip), just do:

var item1 = new ToolStripDropDownButton("my dropdown button");
toolStrip1.Items.Add(item1);

var item2 = new ToolStripProgressBar("my progress bar");
toolStrip1.Items.Add(item2);

// etc ...

EDIT:

You must do it after InitializeComponent() otherwise you won't be able to access to design-time added components, e.g.:

InitializeComponent();

// we're after InitializeComponent...
// let's add 10 buttons under "toolStripDropDownButton1" ...
for (int i = 0; i < 10; i++)
{
    var item = new ToolStripButton("Button_"+i);
    toolStripDropDownButton1.DropDownItems.Add(item);
}
0
Oliver On

For the DropDown property you need a ContextMenuStrip. The easiest way to find out how to fill it up is to drag&drop one from the toolbox onto your form, fill it up, select it in the DropDown property and afterwards take a look into the Designer.cs file to see how all the stuff is glued together.

The drawback of using the DropDownItems property is that you can't alter some properties like ShowImageMargin.