Dynamic Menu Creation In Windows (c#)
Download: dynamicmenuscreation.zip
Introduction
· Menus make it easy to access the functionality and take less space and make your application look more organized.
· Menus contain top – level items and drop – down items.
o Top – level items: all visible items are top-level menus
o Drop – down items: non – visible items are dropdown menus and also call it as child menus of a top – level menu.
Classes required to create a Menu
1. MenuStrip:
· This control is like a container will hold all the top – level menus going to be created.
· This control will be available under “System.Windows.Forms” namespace.
Decalration of a MenuStrip control
Import the namespace – using System.Windows.Forms
// Declare the menustrip object.
MenuStrip menuItems = new MenuStrip();
2. ToolStripMenuItem :
· This class is used to create menu items like top – level or drop – down menus going to be created.
· It can also represent a submenu of another ToolStripMenuItem object. ToolStripMenuItem objects are viewed by the user, whereas a MenuStrip object simply establishes a container where menu items appear.
· This class will be available under “System.Windows.Forms” namespace.
Decalration of a MenuItem class
Menu can be created using a Text or Image or both.
// Take a ToolStripMenuItem to add the menu item with string.
ToolStripMenuItem menuItem = new ToolStripMenuItem(“File”);
Or
// Take a ToolStripMenuItem to add the menu item with Image.
ToolStripMenuItem menuItem = new ToolStripMenuItem(new Bitmap(“path of the image to load”));
Or
// Take a ToolStripMenuItem to add the menu item with Strng and Image.
ToolStripMenuItem menuItem = new ToolStripMenuItem((“File”, new Bitmap(“path of the image to load”));
These are the main classes to create any kind of menu.
Using this information will know how to create dynamic menus.
Program: Going to create dynamic simple menus
Output: looks like this
File Edit Help - Top – level menus
New Copy AboutMe - drop – down menus
Open Cut ContactUs
Exit Pate Help
Step-By-Step code explanation to create a dynamic menu
1. Define menus required.
Note: There will be so many ways to approach declaring the menu items but here I used by taking dictionary object.
// Create Main menu and child menus in a ditcionary object as key pair values.
Dictionary<string, string[]> displayMenus = new Dictionary<string, string[]>();
// Define the child menus based on the each main menu on the basis of key.
displayMenus.Add(“File”, new string[] { “New”, “Open”, “Exit” });
displayMenus.Add(“Edit”, new string[] { “Copy”, “Cut”, “Paste” });
displayMenus.Add(“Help”, new string[] { “AboutMe”, “ContactUs”, “Help” });
2. Write a function to create menus by taking the dictionary object as parameter.
/// <summary>
/// This method will create a menu based on the menus
/// and it’s submenus defined.
/// </summary>
/// <param name=”displayMenus”>
/// Menus and it’s submenus in a dictioanry object
/// </param>
/// <returns>
/// Returns a collection of a toolstripmennu items.
/// </returns>
private List<ToolStripMenuItem> CreateMenu(Dictionary<string, string[]> displayMenus)
{
// Declare ToolStripMenuItem object.
List<ToolStripMenuItem> menuItems = new List<ToolStripMenuItem>();
// Loop through all main menus.
foreach (KeyValuePair<string, string[]> menu in displayMenus)
{
// Take a ToolStripMenuItem to add the menu item.
ToolStripMenuItem menuItem = new ToolStripMenuItem(menu.Key);
// Set a name to the menu.
menuItem.Name = menu.Key;
// Create child menu items for a menu item.
this.CreateChildMenus(menuItem, menu.Value);
switch (menu.Key)
{
case “File”:
case “Edit”:
// This is by default.
// menuItem.Alignment = ToolStripItemAlignment.Left;
break;
case “Help”:
menuItem.Alignment = ToolStripItemAlignment.Right;
break;
}
// Add each menu item to the menu strip item.
menuItems.Add(menuItem);
}
return menuItems;
}
3. Write a function to create child menus by taking the parent menu and the child menus going to be created.
/// <summary>
/// Thsi method will create a child menus of a menu item.
/// </summary>
/// <param name=”parentMenuToAddChildMenus”>
/// Parent menu item to add child menu items.
/// </param>
/// <param name=”childMenus”>
/// Child menu items going to be created.
/// </param>
private void CreateChildMenus(ToolStripMenuItem parentMenuToAddChildMenus, string[] childMenus)
{
// Loop through all child menus.
foreach (string childMenu in childMenus)
{
// Take a ToolStripMenuItem to add the menu item.
ToolStripMenuItem childMenuItem = new ToolStripMenuItem(childMenu);
// Set a name to the menu.
childMenuItem.Name = childMenu;
// Hnadle the event for the menu created.
childMenuItem.Click += new EventHandler(ChildMenu_Click);
// Add each child menu to its parent menu item.
parentMenuToAddChildMenus.DropDown.Items.Add(childMenuItem);
}
}
4. Write an event handler common to all the menu items.
/// <summary>
/// This method will handle the click event for each menu.
/// </summary>
/// <param name=”sender”>Tool strip menu item.</param>
/// <param name=”e”>Event args.</param>
private void ChildMenu_Click(object sender, EventArgs e)
{
ToolStripMenuItem sourceMenuItem = (ToolStripMenuItem)sender;
string selectedMenu = string.Empty;
// Selected menu item
switch (sourceMenuItem.Name)
{
case “New”:
// Required statements here.
selectedMenu = sourceMenuItem.Text;
break;
case “Open”:
// Required statements here.
selectedMenu = sourceMenuItem.Text;
break;
case “Exit”:
// Required statements here.
selectedMenu = sourceMenuItem.Text;
break;
case “Edit”:
// Required statements here.
selectedMenu = sourceMenuItem.Text;
break;
case “Copy”:
// Required statements here.
selectedMenu = sourceMenuItem.Text;
break;
case “Paste”:
// Required statements here.
selectedMenu = sourceMenuItem.Text;
break;
case “AboutMe”:
// Required statements here.
selectedMenu = sourceMenuItem.Text;
break;
case “ContactUs”:
// Required statements here.
selectedMenu = sourceMenuItem.Text;
break;
case “Help”:
// Required statements here.
selectedMenu = sourceMenuItem.Text;
break;
}
if (!string.IsNullOrEmpty(selectedMenu))
{
MessageBox.Show(string.Concat(
selectedMenu,
” feature is under development “));
}
}
5. add the created menu items collection to the MenuStrip object
Conclusion:
· This is the procedure to create dynamic menus in windows application.
· If you want to create your custom type dynamic menus then modify the code according to your needs in a dictionary object as defined in step1.
Note: This is just a sample code to guide you how a menu can be created.
Download: dynamicmenuscreation.zip
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.


Comments
No comments yet.
Leave a comment