With this MenuStrip
control, we can add a menu area and then add the default menus or create personalized menus directly in Visual Stdio. MenuStrip is a menu bar to the program Windows Forms.
The MenuStrip class is the basis for Windows Form menu functionality. You need to be familiar with the mainMenu controls if you have worked with .NET 1.0 and 2.0 menus. The MainMenu
control is substituted by the MenuStrip
control for .NET 3.5 and 4.0.
Menus expose our users to their functions by holding commands grouped according to a common theme.
We can use the Form Designer in design time to create a MenuStrip control or dynamically or in running code using the MenuStrip class.
We simply drag and drop the MenuStrip control from Toolbox to a Visual Studio for creating a MenuStrip control in design time.
When a MenuStrip is dragged to a Form, the MenuStrip1 is added to the Form.
Once the form has a MenuStrip we can add menu items and set their properties and events.
The next step is to set properties, after placing a MenuStrip control on a form. such as:
Name Property
MainMenu.Name = "MainMenu";
A unique name of the MenuStrip control is the name of the property. It is used for accessing the code control.
MenuStrip Position
Dock property is used to set the MenuStrip position. It is DockStyle, with Top, Bottom, Left, Right, and Fill values.
MainMenu.Dock = DockStyle.Left;
Font
Font Property is the MenuStrip controller text font. If we click the Font property, we will see the Name, Size and other Font options in Properties Window.
MainMenu.Font = new Font("Segoe UI", 10);
Background and Foreground
The background and foreground color of a MenuStrip is used in BackColor
and ForeColor
. By clicking on these properties, the Color Dialog will appear in the Properties window.
MainMenu.BackColor = Color.DarkGray;
MainMenu.ForeColor = Color.Black;
Properties window is the easiest method to set properties. By pressing F4 or right-clicking on a control, we can open the Properties window and select the Properties items and click "Collection".
Clicking on the Collections will pop up the Items Collection Editor window, where the strings can be type. The MenuStrip item will be adding every line to this collection.
Add Drop Down Menu Items
We can add Drop Down Items (Sub menu) same as menu item after click on DropDownItem
property Collection option.
Menu Item Click Event Handler
A menu item is mainly designed to add a click on the event handler and write code to run the event handler in the menu item.
For Example:
On File menu
File >> New File sub menu item click event handler, Maybe we want a new file to create.
We go to the event window and double-click On Click to add an event handler.
Creating a runtime MenuStrip control is simply a task of creating a MenuStrip class instance, setting its feats and adding MenuStrip to the Form form.
The first step to creating a MenuStrip dynamic is to create a MenuStrip class instance.
MenuStrip MainMenu = new MenuStrip();
In the next step, we can set MenuStrip control characteristics. This code snippet specifies the properties for a MenuStrip of the background color, foreground color, text, names and fonts.
MainMenu.BackColor = Color.OrangeRed;
MainMenu.ForeColor = Color.Black;
MainMenu.Text = "File Menu";
MainMenu.Font = new Font("Georgia", 16);
The next step is to add the MenuStrip to the form once the MenuStrip control is ready. To this end we first set MainMenuStrip, then use Form.Controls.Add
to the Form Controls by adding the MenuStrip controls and the Form displays based on location and control size.
The following code is added to the current form by adding a MenuStrip control.
MainMenuStrip = MainMenu;
Controls.Add(MainMenu);
A ToolStripMenuItem
shows items from the menu. A menu item is created by the following code snippet and its properties.
ToolStripMenuItem FileMenu = new ToolStripMenuItem("File");
FileMenu.BackColor = Color.OrangeRed;
FileMenu.ForeColor = Color.Black;
FileMenu.Text = "File Menu";
FileMenu.Font = new Font("Georgia", 11);
FileMenu.TextAlign = ContentAlignment.BottomRight;
FileMenu.ToolTipText = "Click Me";
Once a menu item is created, the MenuStrip.Items.Add
method can be added to the main menu.
FileMenu item is added to the MainMenu using the following code snippet.
MainMenu.Items.Add(FileMenu);
ToolStripMenuItem displays items in the menu. A menu item is created by the following snipet code, which sets its properties.
ToolStripMenuItem FileMenu = new ToolStripMenuItem("File");
FileMenu.BackColor = Color.White;
FileMenu.ForeColor = Color.Black;
FileMenu.Text = "File Menu";
FileMenu.Font = new Font("Segoe UI", 10);
FileMenu.TextAlign = ContentAlignment.BottomRight;
FileMenu.ToolTipText = "Click Me";
Using the method MenuStrip.Items.Add
, when a menu item is created, we can add it to the main menu.
MainMenu.Items.Add(FileMenu);
Menu Item Click Event Handler Run Time
An event manager can also be dynamically defined and implemented. This event and their respective events manager are defined and implementated with the following snipet code.
FileMenu.Click += new EventHandler(this.FileMenuItemClick);
private void FileMenuItemClick(object sender, EventArgs e)
{
MessageBox.Show("File menu item clicked");
}
Complete Source Code
using System;
using System.Drawing;
using System.Windows.Forms;
namespace MenuStripControl
{
public partial class MenuStripAtDesignTime : Form
{
public MenuStripAtDesignTime()
{
InitializeComponent();
}
private void FileMenu_Click(object sender, EventArgs e)
{
MessageBox.Show("File Menu item clicked.");
}
private void MenuStripAtDesignTime_Load(object sender, EventArgs e)
{
MainMenu.BackColor = Color.DarkGray;
MainMenu.ForeColor = Color.Black;
MainMenu.Text = "Main Menu";
MainMenu.Font = new Font("Segoe UI", 10);
FileMenu.BackColor = Color.White;
FileMenu.ForeColor = Color.Black;
FileMenu.Text = "File Menu";
FileMenu.Font = new Font("Segoe UI", 10);
FileMenu.TextAlign = ContentAlignment.BottomRight;
FileMenu.ToolTipText = "Click Me";
}
}
}
Output: