Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How can I populate a ToolStripDropDownItem?

ToolStripDropDownItem is the abstract base class for ToolStripMenuItem, ToolStripDropDownButton, ToolStripSplitButton and provides the API around populating and synching the dropdown and items within it.

This class implements all the dropdown plumbing and API including the DropDownItems collection and DropDown property. This is the base class for ToolStripMenuItem, ToolStripSplitButton, and ToolStripDropDownButton.

DropDownItemClicked, DropDownItem

You can populate a dropdown via two different methods – either by hydrating the DropDownItems collection or assigning a created ContextMenuStrip to the ToolStripDropDownItem's 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 DropDown's click event. And you don't 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);


Page view counter