dotnetmonitor.com |
|
||||||||||||
| You can populate a dropdown via two different methods
either by hydrating the DropDownItems collection or assigning a created ContextMenuStrip
to the ToolStripDropDownItems DropDown property. Another handy feature of ToolStripDropDownItem
is the DropDownItemClicked event. This allows you a handy way to not have to sync
each item in a ToolStrip DropDowns click event. And you dont have to fish into
the collection either, we just hand you back the item that was clicked on. The following
sample shows these concepts.
// populate the DropDownItems Collection ToolStripMenuItem veggiesMenuItem = new ToolStripMenuItem("Veggies"); veggiesMenuItem.DropDownItems.Add("Asparagus"); veggiesMenuItem.DropDownItems.Add("Bok Choy"); veggiesMenuItem.DropDownItems.Add("Cauliflower"); // Hook up the handler veggiesMenuItem.DropDownItemClicked += new ToolStripItemClickedEventHandler(myDropDownItemClicked);
// assign a dropdown ContextMenuStrip cms = new ContextMenuStrip(); cms.Items.Add("Apples"); cms.Items.Add("Bananas"); cms.Items.Add("Cherries");
ToolStripMenuItem fruitMenuItem = new ToolStripMenuItem("Fruit"); fruitMenuItem.DropDown = cms; // Hook up the handler fruitMenuItem.DropDownItemClicked += new ToolStripItemClickedEventHandler(myDropDownItemClicked);
// menustrip MenuStrip ms = new MenuStrip(); ms.Items.Add(fruitMenuItem); ms.Items.Add(veggiesMenuItem); this.Controls.Add(ms); |
||||||||||||
|
||||||||||||