MenuStrip In C#

The MenuStrip class is the foundation of menus functionality in Windows Forms. If you have worked with menus in .NET 1.0 and 2.0, you must be familiar with the MainMenu control. In .NET 3.5 and 4.0, the MainMenu control is replaced with the MenuStrip control.

Creating a MenuStrip

We can create a MenuStrip control using a Forms designer at design-time or using the MenuStrip class in code at run-time or dynamically.

To create a MenuStrip control at design-time, you simply drag and drop a MenuStrip control from Toolbox to a Form in Visual Studio. After you drag and drop a MenuStrip on a Form, the MenuStrip1 is added to the Form and looks like Figure 1. Once a MenuStrip is on the Form, you can add menu items and set its properties and events.

MenuStrip
Figure 1

Creating a MenuStrip control at run-time is merely a work of creating an instance of MenuStrip class, setting its properties and adding MenuStrip class to the Form controls.

The first step to create a dynamic MenuStrip is to create an instance of MenuStrip class. The following code snippet creates a MenuStrip control object.

C# Code

  1. MenuStrip MainMenu = new MenuStrip();  

VB.NET Code

  1. Dim MainMenu AsNewMenuStrip()  

In the next step, you may set properties of a MenuStrip control. The following code snippet sets background color, foreground color, Text, Name, and Font properties of a MenuStrip.

C# Code

  1. MainMenu.BackColor = Color.OrangeRed;  
  2. MainMenu.ForeColor = Color.Black;  
  3. MainMenu.Text = "File Menu";  
  4. MainMenu.Font = newFont("Georgia", 16);  

VB.NET Code

  1. MainMenu.BackColor = Color.OrangeRed  
  2. MainMenu.ForeColor = Color.Black  
  3. MainMenu.Text = "File Menu"  
  4. MainMenu.Font = NewFont("Georgia", 16)  

Once the MenuStrip control is ready with its properties, the next step is to add the MenuStrip to a Form. To do so, first we set MainMenuStrip property and then use Form.Controls.Add method that adds MenuStrip control to the Form controls and displays on the Form based on the location and size of the control. The following code snippet adds a MenuStrip control to the current Form.

C# Code

  1. this.MainMenuStrip = MainMenu;  
  2. Controls.Add(MainMenu);  

VB.NET Code

  1. Me.MainMenuStrip = MainMenu  
  2. Controls.Add(MainMenu)  

Setting MenuStrip Properties

After you place a MenuStrip control on a Form, the next step is to set properties.

The easiest way to set properties is from the Properties Window. You can open Properties window by pressing F4 or right clicking on a control and selecting Properties menu item. The Properties window looks like Figure 2.

MenuStrip Properties
Figure 2

Name

Name property represents a unique name of a MenuStrip control. It is used to access the control in the code. The following code snippet sets and gets the name and text of a MenuStrip control.

C# Code

  1. MainMenu.Name = "MainMenu";  

VB.NET Code

  1. MainMenu.Name = "MailMenu"  

Positioning a MenuStrip

The Dock property is used to set the position of a MenuStrip. It is of type DockStyle that can have values Top, Bottom, Left, Right, and Fill. The following code snippet sets Location, Width, and Height properties of a MenuStrip control.

C# Code

  1. MainMenu.Dock = DockStyle.Left;  

VB.NET Code

  1. MainMenu.Dock = DockStyle.Left  

Font

Font property represents the font of text of a MenuStrip control. If you click on the Font property in Properties window, you will see Font name, size and other font options. The following code snippet sets Font property at run-time.

C# Code

  1. MainMenu.Font = newFont("Georgia", 16);  

VB.NET Code

  1. MainMenu.Font = newFont("Georgia", 16)  

Background and Foreground

BackColor and ForeColor properties are used to set background and foreground color of a MenuStrip respectively. If you click on these properties in Properties window, the Color Dialog pops up.

Alternatively, you can set background and foreground colors at run-time. The following code snippet sets BackColor and ForeColor properties.

C# Code

  1. MainMenu.BackColor = Color.OrangeRed;  
  2. MainMenu.ForeColor = Color.Black;  

VB.NET Code

  1. MainMenu.BackColor = Color.OrangeRed  
  2. MainMenu.ForeColor = Color.Black  

The new MenuStrip with background and foreground looks like Figure 3.

MenuStrip with background and foreground
Figure 3

MenuStrip Items

A Menu control is nothing without menu items. The Items property is used to add and work with items in a MenuStrip. We can add items to a MenuStrip at design-time from Properties Window by clicking on Items Collection as you can see in Figure 4.

MenuStrip Items
Figure 4

When you click on the Collections, the String Collection Editor window will pop up where you can type strings. Each line added to this collection will become a MenuStrip item. I add four items as you can see from Figure 5.

MenuStrip Items
Figure 5

A ToolStripMenuItem represents a menu items. The following code snippet creates a menu item and sets its properties.

C# Code

  1. // Create a Menu Item  
  2. ToolStripMenuItem FileMenu = newToolStripMenuItem("File");  
  3. FileMenu.BackColor = Color.OrangeRed;  
  4. FileMenu.ForeColor = Color.Black;  
  5. FileMenu.Text = "File Menu";  
  6. FileMenu.Font = newFont("Georgia", 16);  
  7. FileMenu.TextAlign = ContentAlignment.BottomRight;  
  8. FileMenu.ToolTipText = "Click Me";  

VB.NET Code

  1. Dim FileMenu AsNewToolStripMenuItem("File")  
  2. FileMenu.BackColor = Color.OrangeRed  
  3. FileMenu.ForeColor = Color.Black  
  4. FileMenu.Text = "File Menu"  
  5. FileMenu.Font = NewFont("Georgia", 16)  
  6. FileMenu.TextAlign = ContentAlignment.BottomRight  
  7. FileMenu.TextDirection = ToolStripTextDirection.Vertical90  
  8. FileMenu.ToolTipText = "Click Me"  

Once a menu item is created, we can add it to the main menu by using MenuStrip.Items.Add method. The following code snippet adds FileMenu item to the MainMenu.

C# Code

  1. MainMenu.Items.Add(FileMenu);  

VB.NET Code

  1. MainMenu.Items.Add(FileMenu)  

Adding Menu Item Click Event Handler

The main purpose of a menu item is to add a click event handler and write code that we need to execute on the menu item click event handler. For example, on File >> New menu item click event handler, we may want to create a new file.

To add an event handler, you go to Events window and double click on Click and other as you can see in Figure 6.

Adding Menu Item Click Event Handler
Figure 6

We can also define and implement an event handler dynamically. The following code snippet defines and implements these events and their respective event handlers.

C# Code

  1. FileMenu.Click += new System.EventHandler(this.FileMenuItemClick);  
  2. privatevoid FileMenuItemClick(object sender, EventArgs e) {  
  3.     MessageBox.Show("File menu item clicked");  
  4. }  

VB.NET Code

  1. Dim FileMenuItem AsNewToolStripMenuItem("File", Nothing, _  
  2. NewEventHandler(AddressOf FileMenuItemClick))  
  3. PrivateSub FileMenuItemClick (ByVal sender AsObject, ByVal e AsEventArgs)  
  4. MessageBox.Show("File menu item clicked!")  
  5. EndSub  

Summary

In this article, we discussed how to create menus using the MenuStrip control. First we discussed how to create menus at design-time and run-time. After that we saw how to set menu properties and click event handlers.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.