Creating Run-Time Menus in C#

Tools Used: .NET Framework, EditPlus.

runtime menu in c#

The following example demonstrates how to add menu item and remove menu items at runtime.

  1. /*  
  2. Author - Mokhtar B 
  3. Date - 13th June, 2001 
  4. Company - Adsoft Solutions Pvt. Ltd  
  5. Application Type - Windows Forms 
  6. */  
  7. using System;  
  8. using System.WinForms;  
  9. using System.Drawing;  
  10. //<Summary>  
  11. ///This application demonstrates how to create Menus at runtime.  
  12. ///</Summary>   
  13. class RuntimeMenus:Form  
  14. {  
  15. private MainMenu myMenu;  
  16. private MenuItem mnuFile, mnuExit, mnuRtime;  
  17. private Button btnAdd, btnRemove, btnClose;  
  18. private Graphics g;  
  19. private Font DispFont;  
  20. private SolidBrush MyBrush;  
  21. static int i;  
  22. ///Constructor Calls InitializeComponents method, which is used  
  23. ///for initializing components that are used in the application  
  24. public RuntimeMenus()  
  25. {  
  26. InitializeComponents();  
  27. }  
  28. public void InitializeComponents()  
  29. {  
  30. DispFont = new Font("Verdana",8,FontStyle.Bold);  
  31. MyBrush = new SolidBrush(Color.Blue);  
  32. //Instantiating Main Menu  
  33. myMenu = new MainMenu();  
  34. //Instantiating Menus  
  35. mnuFile = new MenuItem("&File");  
  36. mnuRtime = new MenuItem("&Runtime Menu");  
  37. //Adding menus to Main Menu  
  38. myMenu.MenuItems.Add(mnuFile);  
  39. myMenu.MenuItems.Add(mnuRtime);  
  40. //Instantiating Exit menuitem along with a shortcut key.  
  41. mnuExit = new MenuItem("&Exit"new EventHandlerbtnClose_Click),Shortcut.CtrlX);  
  42. //Adding menuitem to File Menu  
  43. mnuFile.MenuItems.Add(mnuExit);  
  44. //Instantiating Add Button  
  45. btnAdd = new Button();  
  46. btnAdd.Location = new Point(275,20);  
  47. btnAdd.Text = "&Add";  
  48. btnAdd.Click += new EventHandler(btnAdd_Click);  
  49. btnAdd.Size = new Size(100,30);  
  50. //Instantiating Remove Button  
  51. btnRemove = new Button();  
  52. btnRemove.Location = new Point(275,60);  
  53. btnRemove.Text = "&Remove";  
  54. btnRemove.Click += new EventHandler(btnRemove_Click);  
  55. btnRemove.Size = new Size(100,30);  
  56. //Instantiating Close Button  
  57. btnClose = new Button();  
  58. btnClose.Location = new Point(275,100);  
  59. btnClose.Text = "&Close";  
  60. btnClose.Click += new EventHandler(btnClose_Click);  
  61. btnClose.Size = new Size(100,30);  
  62. //Adding Buttons to Form  
  63. this.Controls.Add(btnAdd);  
  64. this.Controls.Add(btnRemove);  
  65. this.Controls.Add(btnClose);  
  66. //Setting properties of the Form  
  67. this.MaximizeBox = false;  
  68. this.MinimizeBox = false;  
  69. this.Text = "Creating Menus on the Fly";  
  70. this.Menu = myMenu;  
  71. this.Size = new Size(400,250);  
  72. this.CancelButton = btnClose;  
  73. this.StartPosition = FormStartPosition.CenterScreen;  
  74. //Initializing Graphics from Windows Handle  
  75. g = Graphics.FromHWND(this.Handle);  
  76. }  
  77. ///Adds Menuitems to RuntimeMenu  
  78. protected void btnAdd_Click(object sender, EventArgs e)  
  79. {  
  80. mnuRtime.MenuItems.Add("Runtime Menu " + (i+1), new EventHandler mnuRtime_Click));  
  81. g.Clear(Color.FromA#d8d0c8);  
  82. g.DrawString("Runtime Menu " + (i + 1) + " added Successfully",DispFont,MyBrush,50,150);  
  83. i++;  
  84. }  
  85. ///Removes Menuitems from RuntimeMenu  
  86. protected void btnRemove_Click(object sender, EventArgs e)  
  87. {  
  88. if (i <= 0)  
  89. {  
  90. g.Clear(Color.FromA#d8d0c8);  
  91. g.DrawString("Menu is Empty",DispFont, MyBrush,125,150);  
  92. }  
  93. else  
  94. {  
  95. mnuRtime.MenuItems.Remove(i-1);  
  96. g.Clear(Color.FromA#d8d0c8);  
  97. g.DrawString("Runtime Menu " + (i) + " Removed Successfully",DispFont,MyBrush,50,150);  
  98. i=i-1;  
  99. }  
  100. }  
  101. //Event for Displaying the MessageBox on Clicking  
  102. protected void mnuRtime_Click(object sender, EventArgs e)  
  103. {  
  104. String s = sender.ToString();  
  105. MessageBox.Show("You Clicked " + s.Substring(28), "RunTime Menu", MessageBox.IconInformation);  
  106. }  
  107. ///Event for Closing Window  
  108. protected void btnClose_Click(object sender, EventArgs e)  
  109. {  
  110. Application.Exit();  
  111. }  
  112. ///Instantiating the Application  
  113. public static void Main()  
  114. {  
  115. Application.Run(new RuntimeMenus());  
  116. }  
  117. }