Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / C#

Improvement of the .NET Menu Style Class

Rate me:
Please Sign up or sign in to vote.
4.27/5 (17 votes)
14 Jan 20032 min read 211.2K   467   51  
This is an improvement by Francesco Natali over an improvement by Sajith M of the "Visual Studio .NET Menu Style" by Carlos H. Perez
using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;
// ---------------------
using Utility.NiceMenu; 
// ---------------------

namespace MenuSample
{
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.MenuItem menuItem5;
		private System.Windows.Forms.MenuItem menuItem7;
		private System.Windows.Forms.MenuItem mnuProgramma;
		private System.Windows.Forms.MenuItem mnuEsegui;
		private System.Windows.Forms.MenuItem mnuEsci;
		private System.Windows.Forms.MenuItem mnuHelp;
		private System.Windows.Forms.MenuItem mnuGuida;
		private System.Windows.Forms.MenuItem mnuInformazioni;
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem menuItem6;
		private System.Windows.Forms.MenuItem menuItem8;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.ContextMenu contextMenu1;
		private System.Windows.Forms.MenuItem menuItem9;
		private System.Windows.Forms.MenuItem menuItem10;
		private System.Windows.Forms.MenuItem menuItem11;
		private System.Windows.Forms.MenuItem menuItem12;
		private System.Windows.Forms.MenuItem menuItem13;
		private System.Windows.Forms.MenuItem menuItem14;
		private System.Windows.Forms.MenuItem menuItem15;
		private System.Windows.Forms.MenuItem menuItem16;
		private System.ComponentModel.IContainer components;

		public Form1()
		{
			InitializeComponent();
			// --------------------------------------------
			NiceMenu myNewNiceMenu = new NiceMenu();
			NiceMenu myNewNiceContextMenu = new NiceMenu();
			// --------------------------------------------
			myNewNiceMenu.MenuImages = imageList1;
			myNewNiceContextMenu.MenuImages = imageList1;
			// --------------------------------------------
			myNewNiceMenu.UpdateMenu(this.mainMenu, new NiceMenuClickEvent(NiceMenuClickEvent));
			myNewNiceContextMenu.UpdateMenu(this.contextMenu1, new NiceMenuClickEvent(NiceMenuClickEvent));
			// --------------------------------------------
			myNewNiceMenu.SelectionColor = Color.AliceBlue;
			myNewNiceContextMenu.SelectionColor = Color.AliceBlue;
		}

		public void NiceMenuClickEvent(object sender, System.EventArgs e)
		{
			NiceMenu item = (NiceMenu)sender;
			MessageBox.Show(item.Text);
			if (item.Text == "Exit")
			{
				this.Dispose(true);
				Application.Exit();
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.mnuProgramma = new System.Windows.Forms.MenuItem();
			this.mnuEsegui = new System.Windows.Forms.MenuItem();
			this.menuItem7 = new System.Windows.Forms.MenuItem();
			this.mnuEsci = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem6 = new System.Windows.Forms.MenuItem();
			this.menuItem8 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.mnuHelp = new System.Windows.Forms.MenuItem();
			this.mnuGuida = new System.Windows.Forms.MenuItem();
			this.menuItem5 = new System.Windows.Forms.MenuItem();
			this.mnuInformazioni = new System.Windows.Forms.MenuItem();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.contextMenu1 = new System.Windows.Forms.ContextMenu();
			this.menuItem9 = new System.Windows.Forms.MenuItem();
			this.menuItem10 = new System.Windows.Forms.MenuItem();
			this.menuItem11 = new System.Windows.Forms.MenuItem();
			this.menuItem12 = new System.Windows.Forms.MenuItem();
			this.menuItem13 = new System.Windows.Forms.MenuItem();
			this.menuItem14 = new System.Windows.Forms.MenuItem();
			this.menuItem15 = new System.Windows.Forms.MenuItem();
			this.menuItem16 = new System.Windows.Forms.MenuItem();
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.mnuProgramma,
																					 this.mnuHelp});
			// 
			// mnuProgramma
			// 
			this.mnuProgramma.Index = 0;
			this.mnuProgramma.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.mnuEsegui,
																						 this.menuItem7,
																						 this.mnuEsci,
																						 this.menuItem1});
			this.mnuProgramma.Text = "File";
			// 
			// mnuEsegui
			// 
			this.mnuEsegui.DefaultItem = true;
			this.mnuEsegui.Index = 0;
			this.mnuEsegui.Shortcut = System.Windows.Forms.Shortcut.CtrlE;
			this.mnuEsegui.Text = "01Menu item with image";
			// 
			// menuItem7
			// 
			this.menuItem7.Index = 1;
			this.menuItem7.Text = "-";
			// 
			// mnuEsci
			// 
			this.mnuEsci.Index = 2;
			this.mnuEsci.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					this.menuItem2,
																					this.menuItem3,
																					this.menuItem4});
			this.mnuEsci.Text = "Sub menu example";
			// 
			// menuItem2
			// 
			this.menuItem2.Checked = true;
			this.menuItem2.Index = 0;
			this.menuItem2.Text = "menu A";
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 1;
			this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem6,
																					  this.menuItem8});
			this.menuItem3.Text = "menu B";
			// 
			// menuItem6
			// 
			this.menuItem6.Checked = true;
			this.menuItem6.Index = 0;
			this.menuItem6.RadioCheck = true;
			this.menuItem6.Text = "Radio A";
			// 
			// menuItem8
			// 
			this.menuItem8.Index = 1;
			this.menuItem8.RadioCheck = true;
			this.menuItem8.Text = "Radio B";
			// 
			// menuItem4
			// 
			this.menuItem4.Enabled = false;
			this.menuItem4.Index = 2;
			this.menuItem4.Text = "this menu item is disable";
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 3;
			this.menuItem1.Shortcut = System.Windows.Forms.Shortcut.F1;
			this.menuItem1.ShowShortcut = false;
			this.menuItem1.Text = "Exit";
			// 
			// mnuHelp
			// 
			this.mnuHelp.Index = 1;
			this.mnuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					this.mnuGuida,
																					this.menuItem5,
																					this.mnuInformazioni});
			this.mnuHelp.Text = "?";
			// 
			// mnuGuida
			// 
			this.mnuGuida.Index = 0;
			this.mnuGuida.Text = "Help";
			// 
			// menuItem5
			// 
			this.menuItem5.Index = 1;
			this.menuItem5.Text = "-";
			// 
			// mnuInformazioni
			// 
			this.mnuInformazioni.Index = 2;
			this.mnuInformazioni.Text = "About...";
			// 
			// imageList1
			// 
			this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// contextMenu1
			// 
			this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.menuItem9,
																						 this.menuItem10,
																						 this.menuItem11});
			// 
			// menuItem9
			// 
			this.menuItem9.Index = 0;
			this.menuItem9.Text = "This is a";
			// 
			// menuItem10
			// 
			this.menuItem10.Index = 1;
			this.menuItem10.Text = "context";
			// 
			// menuItem11
			// 
			this.menuItem11.Index = 2;
			this.menuItem11.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.menuItem12,
																					   this.menuItem13,
																					   this.menuItem16});
			this.menuItem11.Text = "menu";
			// 
			// menuItem12
			// 
			this.menuItem12.Index = 0;
			this.menuItem12.Text = "01submenu A";
			// 
			// menuItem13
			// 
			this.menuItem13.Index = 1;
			this.menuItem13.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.menuItem14,
																					   this.menuItem15});
			this.menuItem13.Text = "submenu B";
			// 
			// menuItem14
			// 
			this.menuItem14.Index = 0;
			this.menuItem14.Shortcut = System.Windows.Forms.Shortcut.CtrlA;
			this.menuItem14.Text = "A";
			// 
			// menuItem15
			// 
			this.menuItem15.Index = 1;
			this.menuItem15.Shortcut = System.Windows.Forms.Shortcut.CtrlB;
			this.menuItem15.Text = "B";
			// 
			// menuItem16
			// 
			this.menuItem16.Index = 2;
			this.menuItem16.Text = "submenu C";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 273);
			this.ContextMenu = this.contextMenu1;
			this.Menu = this.mainMenu;
			this.Name = "Form1";
			this.Text = "Form1";

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions