Blue Theme Orange Theme Green Theme Red Theme
 
Discover the top 5 tips for understanding .NET Interop
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
6 Months Free & No Setup Fees ASP.NET Hosting!
Search :       Advanced Search »
Home » Windows Controls C# » MenuStrip in C#

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.

Author Rank :
Page Views : 27945
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 



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.

MenuStripImg1.jpg
Figure 1

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

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:

MenuStrip MainMenu = new MenuStrip();

 

VB.NET Code:

 

Dim MainMenu As New MenuStrip()

 

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:

MainMenu.BackColor = Color.OrangeRed;

MainMenu.ForeColor = Color.Black;

MainMenu.Text = "File Menu";

MainMenu.Font = new Font("Georgia", 16);

 

VB.NET Code:

 

MainMenu.BackColor = Color.OrangeRed

MainMenu.ForeColor = Color.Black

MainMenu.Text = "File Menu"

MainMenu.Font = New Font("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:

this.MainMenuStrip = MainMenu;

Controls.Add(MainMenu);

 

VB.NET Code:

 

Me.MainMenuStrip = MainMenu

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 click on a control and select Properties menu item. The Properties window looks like Figure 2.

MenuStripImg2.jpg
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:

MainMenu.Name = "MainMenu";

 

VB.NET Code:

 

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:

MainMenu.Dock = DockStyle.Left;

 

VB.NET Code:

 

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:

MainMenu.Font = new Font("Georgia", 16);

 

VB.NET Code:

 

MainMenu.Font = new Font("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:

MainMenu.BackColor = Color.OrangeRed;

MainMenu.ForeColor = Color.Black;

 

VB.NET Code:

 

MainMenu.BackColor = Color.OrangeRed

MainMenu.ForeColor = Color.Black

 

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

 

MenuStripImg3.jpg
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.

MenuStripImg4.jpg
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.

MenuStripImg5.jpg
Figure 5

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

C# Code:

// Create a Menu Item

ToolStripMenuItem FileMenu = new ToolStripMenuItem("File");

FileMenu.BackColor = Color.OrangeRed;

FileMenu.ForeColor = Color.Black;

FileMenu.Text = "File Menu";

FileMenu.Font = new Font("Georgia", 16);

FileMenu.TextAlign = ContentAlignment.BottomRight;

FileMenu.ToolTipText = "Click Me";

 

VB.NET Code:

 

Dim FileMenu As New ToolStripMenuItem("File")

FileMenu.BackColor = Color.OrangeRed

FileMenu.ForeColor = Color.Black

FileMenu.Text = "File Menu"

FileMenu.Font = New Font("Georgia", 16)

FileMenu.TextAlign = ContentAlignment.BottomRight

FileMenu.TextDirection = ToolStripTextDirection.Vertical90

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:

MainMenu.Items.Add(FileMenu);

 

VB.NET Code:

 

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.

 


MenuStripImg6.jpg
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:

FileMenu.Click += new System.EventHandler(this.FileMenuItemClick);

 

private void FileMenuItemClick(object sender, EventArgs e)

{

    MessageBox.Show("File menu item clicked");

}

 

 

VB.NET Code:

 

Dim FileMenuItem As New ToolStripMenuItem("File", Nothing, _

                New EventHandler(AddressOf FileMenuItemClick))

 

Private Sub FileMenuItemClick (ByVal sender As Object, ByVal e As EventArgs)

        MessageBox.Show("File menu item clicked!")

End Sub

 

 

Summary
In this article, we discussed discuss 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 menus properties and click event handlers.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
Good work by javed On June 9, 2010
Great artical for beginner it help us lot..please come up more with this type of concept.
Reply | Email | Modify 
Re: Good work by Mahesh On June 12, 2010
Thanks Javed.
Reply | Email | Modify 
Thanks by Alex On December 2, 2010
Excellent tutorial. It didn't answer the question I had but the overview of the Menu Item control is perfect for beginners. Thanks for taking the time to write it.
Reply | Email | Modify 
Discover the top 5 tips for understanding .NET Interop
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.