Introduction
In this article we will see how to create a MenuStrip dynamically and add Items to the MenuStrip control using ToolStripMenuItem in a C# Windows Forms application.
MenuStrip:
The MenuStrip gives us an easier approach to represent and group application commands.
To add a MenuStrip Dynamically at run-time by creating an instance of the MenuStrip Class.
MenuStrip MnuStrip = new MenuStrip();
//Control is added to the Form using the Add property
this.Controls.Add(MnuStrip);
Once added the MenuStrip to the Form, our form looks something like this
Screen view 1:
A MenuStrip is nothing with out MenuItems. To add MenuItems to the MenuStrip we use the
ToolStripMenuItem class.
Code:
string[] row = new string[] { "File", "Edit", "View", "Refactor", "Project" };
foreach (string rw in row)
{
MnuStripItem = new ToolStripMenuItem(rw);
MnuStrip.Items.Add(MnuStripItem);
}
Screen View 2:
The ToolStripMenuItem shows differently depending on whether it is a Top-level Menu Item in
a MenuStrip Control or a sub menu.
When it a Sub-Menu it shows a margin to the left where images are displayed if we have added.
Menu Items are added to the MenuStrip using the Items Collection and we can also add properties
for handling shortcut keys and supporting checked menu items.
To add a Sub Menu to the File Menu, I have using the ToolStripMenuItem I have included this piece of
Code using SubMenu Method.
public void SubMenu(ToolStripMenuItem MnuItems, string var)
{
if (var == "File")
{
string[] row = new string[] { "New", "Open", "Add", "Close", "Close Solution"};
foreach (string rw in row)
{
ToolStripMenuItem SSMenu = new ToolStripMenuItem(rw,null, ChildClick);
// SubMenu(SSMenu,rw); I have included this piece of code to add a Sub-Menu to the New Menu
MnuItems.DropDownItems.Add(SSMenu);
}
}
Screen view 3:
To assign an Event Handler for the code I have included the ChildClick event in the ToolStripMenuItem.
Event Code of ChildClick EventHandler:
public void ChildClick(object sender, System.EventArgs e)
{
MessageBox.Show(string.Concat("You have Clicked '", sender.ToString(), "' Menu"), "Menu Items Event",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
To add a SubMenu to the MenuStrip, we use ToolStripMenuItem by creating an instance of it.
If we need to add Sub-Sub Menu, use create a new Instance of ToolStripMenuItem class and adding
it to the ToolStripMenuItem using the DropDownItems collection and adding it to the ToolStripMenuItem.
In the similar way the Sub Menu's can be nested, by creating a new instance of the ToolStripMenuItem class and
adding it to the existing ToolStripMenuItem instance.
Code:
public void SubMenu(ToolStripMenuItem MnuItems, string var)
{
if (var == "File")
{
string[] row = new string[] { "New", "Open", "Add", "Close", "Close Solution"};
foreach (string rw in row)
{
ToolStripMenuItem SSMenu = new ToolStripMenuItem(rw,null, ChildClick);
SubMenu(SSMenu,rw);
MnuItems.DropDownItems.Add(SSMenu);
}
}
if (var == "New")
{
string[] row = new string[] { "Project", "Web Site", "File..","Project From Existing Code" };
foreach (string rw in row)
{
ToolStripMenuItem SSSMenu = new ToolStripMenuItem(rw,null,ChildClick);
MnuItems.DropDownItems.Add(SSSMenu);
}
}
}
Screen View 4:
Then, eventually adding the Menu Items and Sub-Menu Items to the Form, we use MenuStrip instance and the Items collection to add it to the form.
Code:
MnuStrip.Items.Add(MnuStripItem);
Here MnuStrip is the instance of MenuStrip Class and MnuStripItem an instance of the ToolStripMenuItem class.
Event handler for the Menu-Items and Sub-Menu Items can be Specified in the ToolStripMenuItem class while creating the instance of it. You can also add an mage to the ToolStripMenuItem to the MenuItems by Specifying the image path if needed.
Screen View 5:
Screen View 6:
Summary:
Here we have seen how to add a MenuStrip to a Form dynamically. We also saw how to add Items to the MenuStrip using the ToolStripMenuItem class by creating an
instance of the class, and finally adding the Sub-Menu Items using the MenuStrip instance
created above.