Tools used:

.Net Framework Beta2, Editplus.

Introduction:

The following example demonstrates how to create menus on the fly. Namespaces and Classes used by this example are as follows:

Source Code:

/*
Author Mokhtar B
Date 20th August, 2001
Company Adsoft Solutions Pvt. Ltd
Application Type Windows Forms
*/
using System;
using System.Windows.Forms;
using System.Drawing;
///<Summary>
///
This application demonstrates how to create Menus at runtime.
///</Summary>
class RuntimeMenus:Form
{
private MainMenu myMenu;
private MenuItem mnuFile, mnuExit, mnuRtime;
private Button btnAdd, btnRemove, btnClose;
private Graphics g;
private Font DispFont;
private SolidBrush MyBrush;
static int i;
///Constructor Calls InitializeComponents method, which is used
///for initializing components that are used in the application
public RuntimeMenus()
{
InitializeComponents();
}
public void InitializeComponents()
{
DispFont =
new Font("Verdana",8,FontStyle.Bold);
MyBrush =
new SolidBrush(Color.Blue);
//Instantiating Main Menu
myMenu = new MainMenu();
//Instantiating Menus
mnuFile = new MenuItem("&File");
mnuRtime =
new MenuItem("&Runtime Menu");
//Adding menus to Main Menu
myMenu.MenuItems.Add(mnuFile);
myMenu.MenuItems.Add(mnuRtime);
//Instantiating Exit menuitem along with a shortcut key.
mnuExit = new MenuItem("&Exit", new EventHandler(btnClose_Click), hortcut.CtrlX);
//Adding menuitem to File Menu
mnuFile.MenuItems.Add(mnuExit);
//Instantiating Add Button
btnAdd = new Button();
btnAdd.Location =
new Point(275,20);
btnAdd.Text = "&Add";
btnAdd.Click +=
new EventHandler(btnAdd_Click);
btnAdd.Size =
new Size(100,30);
//Instantiating Remove Button
btnRemove = new Button();
btnRemove.Location =
new Point(275,60);
btnRemove.Text = "&Remove";
btnRemove.Click +=
new EventHandler(btnRemove_Click);
btnRemove.Size = new Size(100,30);
//Instantiating Close Button
btnClose = new Button();
btnClose.Location =
new Point(275,100);
btnClose.Text = "&Close";
btnClose.Click +=
new EventHandler(btnClose_Click);
btnClose.Size =
new Size(100,30);
//Adding Buttons to Form
this.Controls.Add(btnAdd);
this.Controls.Add(btnRemove);
this.Controls.Add(btnClose);
//Setting properties of the Form
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Text = "Creating Menus on the Fly";
this.Menu = myMenu;
this.Size = new Size(400,250);
this.CancelButton = btnClose;
this.StartPosition = FormStartPosition.CenterScreen;
//Initializing Graphics from Windows Handle
g = Graphics.FromHwnd(this.Handle);
}
///Adds Menuitems to RuntimeMenu
protected void btnAdd_Click(object sender, EventArgs e)
{
mnuRtime.MenuItems.Add("Runtime Menu " + (i+1),
new EventHandler
mnuRtime_Click));
g.Clear(Color.FromArgb(214,211,206));
g.DrawString("Runtime Menu " + (i + 1) + " added Successfully",DispFont,MyBrush,50,150);
i++;
}
///Removes Menuitems from RuntimeMenu
protected void btnRemove_Click(object sender, EventArgs e)
{
if (i <= 0)
{
g.Clear(Color.FromArgb(214,211,206));
g.DrawString("Menu is Empty",DispFont, MyBrush,125,150);
}
else
{
mnuRtime.MenuItems.RemoveAt(i-1);
g.Clear(Color.FromArgb(214,211,206));
g.DrawString("Runtime Menu " + (i) + " Removed Successfully",DispFont,MyBrush,50,150);
i=i-1;
}
}
///Event for Displaying the MessageBox on Clicking
protected void mnuRtime_Click(object sender, EventArgs e)
{
String s = sender.ToString();
MessageBox.Show("You Clicked " + s.Substring(51), "RunTime Menu",MessageBoxButtons.OK, MessageBoxIcon.Information);
}
///Event for Closing Window
protected void btnClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
///Instantiating the Application
public static void Main()
{
Application.Run(
new RuntimeMenus());
}
}

Compilation:

csc /t:winexe /r:System.dll /r:System.Windows.Forms.dll /r:System.Drawing.dll rmenus.cs

Output:

On clicking Add button new menu item will be added as follows:



On Clicking Remove button menu item will be removed as follows: