C# Windows Forms MenuStrip Examples

Learn MenuStrip in C# using these simple step by step examples.

What is MenuStrip?

Menus expose functionality to your users by holding commands that are grouped by a common theme.

The MenuStrip control was introduced in version 2.0 of the .NET Framework. With the MenuStrip control, you can easily create menus like those found in Microsoft Office.

The MenuStrip control supports the multiple-document interface (MDI) and menu merging, tool tips, and overflow. You can enhance the usability and readability of your menus by adding access keys, shortcut keys, check marks, images, and separator bars.

MenuStrip Provides a menu system for a form.

public class MenuStrip : System.Windows.Forms.ToolStrip

Here is its Inheritance tree:

Object -> MarshalByRefObject -> Component -> Control -> ScrollableControl -> ToolStrip -> MenuStrip

MenuStrip is the top-level container that supersedes MainMenu. It also provides key handling and multiple document interface (MDI) features. Functionally, ToolStripDropDownItem and ToolStripMenuItem work along with MenuStrip, although they are derived from ToolStripItem.

The following items are specifically designed to work seamlessly with both ToolStripSystemRenderer and ToolStripProfessionalRenderer in all orientations. They are available by default at design time for the MenuStrip control:

  1. ToolStripMenuItem
  2. ToolStripTextBox
  3. ToolStripComboBox

The MenuStrip control represents the container for the menu structure of a form. You can add ToolStripMenuItem objects to the MenuStrip that represent the individual menu commands in the menu structure. Each ToolStripMenuItem can be a command for your application or a parent menu for other submenu items.

The MenuStrip replaces and extends the MainMenu control, which was removed in.NET Core 3.1.

Ways of using MenuStrip

Use the MenuStrip control to:

  1. Create easily customized, commonly employed menus that support advanced user interface and layout features, such as text and image ordering and alignment, drag-and-drop operations, MDI, overflow, and alternate modes of accessing menu commands.
  2. Support the typical appearance and behavior of the operating system.
  3. Handle events consistently for all containers and contained items, in the same way you handle events for other controls.

Let us look at some examples:

Example 1: MenuStrip Example

The following code example demonstrates a MenuStrip in a multiple-document interface (MDI) scenario.

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

This code example demonstrates how to use ToolStripPanel controls with a multiple document interface (MDI):

Start by extending the Form class:


public class Form1 : Form
{

Then define the constructor of the Form1 class:

    public Form1()
    {

Then Make the Form an MDI parent:

        this.IsMdiContainer = true;

Then Create ToolStripPanel controls:

        ToolStripPanel tspTop = new ToolStripPanel();
        ToolStripPanel tspBottom = new ToolStripPanel();
        ToolStripPanel tspLeft = new ToolStripPanel();
        ToolStripPanel tspRight = new ToolStripPanel();

Then Dock the ToolStripPanel controls to the edges of the form:

        tspTop.Dock = DockStyle.Top;
        tspBottom.Dock = DockStyle.Bottom;
        tspLeft.Dock = DockStyle.Left;
        tspRight.Dock = DockStyle.Right;

Create ToolStrip controls to move among the ToolStripPanel controls. Create the "Top" ToolStrip control and add to the corresponding ToolStripPanel:

        ToolStrip tsTop = new ToolStrip();
        tsTop.Items.Add("Top");
        tspTop.Join(tsTop);

Then Create the "Bottom" ToolStrip control and add to the corresponding ToolStripPanel;

        ToolStrip tsBottom = new ToolStrip();
        tsBottom.Items.Add("Bottom");
        tspBottom.Join(tsBottom);

Then Create the "Right" ToolStrip control and add to the corresponding ToolStripPanel:

        ToolStrip tsRight = new ToolStrip();
        tsRight.Items.Add("Right");
        tspRight.Join(tsRight);

Then Create the "Left" ToolStrip control and add to the corresponding ToolStripPanel:

        ToolStrip tsLeft = new ToolStrip();
        tsLeft.Items.Add("Left");
        tspLeft.Join(tsLeft);

Then Create a MenuStrip control with a new window:

        MenuStrip ms = new MenuStrip();
        ToolStripMenuItem windowMenu = new ToolStripMenuItem("Window");
        ToolStripMenuItem windowNewMenu = new ToolStripMenuItem("New", null, new EventHandler(windowNewMenu_Click));
        windowMenu.DropDownItems.Add(windowNewMenu);
        ((ToolStripDropDownMenu)(windowMenu.DropDown)).ShowImageMargin = false;
        ((ToolStripDropDownMenu)(windowMenu.DropDown)).ShowCheckMargin = true;

Then Assign the ToolStripMenuItem that displays the list of child forms:

        ms.MdiWindowListItem = windowMenu;

Then Add the window ToolStripMenuItem to the MenuStrip:

        ms.Items.Add(windowMenu);

Then Dock the MenuStrip to the top of the form:

        ms.Dock = DockStyle.Top;

The Form.MainMenuStrip property determines the merge target:

        this.MainMenuStrip = ms;

Then Add the ToolStripPanels to the form in reverse order:

        this.Controls.Add(tspRight);
        this.Controls.Add(tspLeft);
        this.Controls.Add(tspBottom);
        this.Controls.Add(tspTop);

Then Add the MenuStrip last. This is important for correct placement in the z-order:

        this.Controls.Add(ms);
    }

This event handler is invoked when the "New" ToolStripMenuItem is clicked. It creates a new Form and sets its MdiParent property to the main form.

    void windowNewMenu_Click(object sender, EventArgs e)
    {
        Form f = new Form();
        f.MdiParent = this;
        f.Text = "Form - " + this.MdiChildren.Length.ToString();
        f.Show();
    }
}

Here is the full code example:

public class Form1 : Form
{
    public Form1()
    {
        this.IsMdiContainer = true;

        ToolStripPanel tspTop = new ToolStripPanel();
        ToolStripPanel tspBottom = new ToolStripPanel();
        ToolStripPanel tspLeft = new ToolStripPanel();
        ToolStripPanel tspRight = new ToolStripPanel();

        tspTop.Dock = DockStyle.Top;
        tspBottom.Dock = DockStyle.Bottom;
        tspLeft.Dock = DockStyle.Left;
        tspRight.Dock = DockStyle.Right;

        ToolStrip tsTop = new ToolStrip();
        tsTop.Items.Add("Top");
        tspTop.Join(tsTop);

        ToolStrip tsBottom = new ToolStrip();
        tsBottom.Items.Add("Bottom");
        tspBottom.Join(tsBottom);

        ToolStrip tsRight = new ToolStrip();
        tsRight.Items.Add("Right");
        tspRight.Join(tsRight);

        ToolStrip tsLeft = new ToolStrip();
        tsLeft.Items.Add("Left");
        tspLeft.Join(tsLeft);

        MenuStrip ms = new MenuStrip();
        ToolStripMenuItem windowMenu = new ToolStripMenuItem("Window");
        ToolStripMenuItem windowNewMenu = new ToolStripMenuItem("New", null, new EventHandler(windowNewMenu_Click));
        windowMenu.DropDownItems.Add(windowNewMenu);
        ((ToolStripDropDownMenu)(windowMenu.DropDown)).ShowImageMargin = false;
        ((ToolStripDropDownMenu)(windowMenu.DropDown)).ShowCheckMargin = true;

        ms.MdiWindowListItem = windowMenu;

        ms.Items.Add(windowMenu);

        ms.Dock = DockStyle.Top;

        this.MainMenuStrip = ms;

        this.Controls.Add(tspRight);
        this.Controls.Add(tspLeft);
        this.Controls.Add(tspBottom);
        this.Controls.Add(tspTop);

        this.Controls.Add(ms);
    }

    void windowNewMenu_Click(object sender, EventArgs e)
    {
        Form f = new Form();
        f.MdiParent = this;
        f.Text = "Form - " + this.MdiChildren.Length.ToString();
        f.Show();
    }
}

Example 2: MenuStrip

This example will comprise the following files:

  • MenuStrip.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). MenuStrip.cs

Create a file named MenuStrip.cs

Here is the full code

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Examples {
  public class Form1 : Form {
    // The main entry point for the application.
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

    public Form1() {
      this.menuSrtrip.Parent = this;
      this.menuSrtrip.Items.Add("File");
      this.menuSrtrip.Items.Add("Edit");
      this.menuSrtrip.Items.Add("View");

      (this.menuSrtrip.Items[0] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("New", null, this.OnMenuClick, Keys.N| Keys.Control));
      (this.menuSrtrip.Items[0] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Open", null, this.OnMenuClick, Keys.O | Keys.Control));
      (this.menuSrtrip.Items[0] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Open recent...", null, this.OnMenuClick));
      (this.menuSrtrip.Items[0] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Close", null, this.OnMenuClick, Keys.W | Keys.Control));
      (this.menuSrtrip.Items[0] as ToolStripMenuItem).DropDown.Items.Add("-");
      (this.menuSrtrip.Items[0] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Save", null, this.OnMenuClick, Keys.S | Keys.Control));
      (this.menuSrtrip.Items[0] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Save as...", null, this.OnMenuClick));
      (this.menuSrtrip.Items[0] as ToolStripMenuItem).DropDown.Items.Add("-");
      (this.menuSrtrip.Items[0] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Quit", null, this.OnMenuClick, Environment.OSVersion.Platform >= PlatformID.Unix ? Keys.Q | Keys.Control : Keys.F4 | Keys.Alt));

      (this.menuSrtrip.Items[1] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Undo", null, this.OnMenuClick, Keys.Z | Keys.Control));
      (this.menuSrtrip.Items[1] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Redo", null, this.OnMenuClick, Keys.Y | Keys.Control));
      (this.menuSrtrip.Items[1] as ToolStripMenuItem).DropDown.Items.Add("-");
      (this.menuSrtrip.Items[1] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Cut", null, this.OnMenuClick, Keys.X | Keys.Control));
      (this.menuSrtrip.Items[1] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Copy", null, this.OnMenuClick, Keys.C | Keys.Control));
      (this.menuSrtrip.Items[1] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Paste", null, this.OnMenuClick, Keys.V | Keys.Control));
      (this.menuSrtrip.Items[1] as ToolStripMenuItem).DropDown.Items.Add("-");
      (this.menuSrtrip.Items[1] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Select all", null, this.OnMenuClick, Keys.A | Keys.Control));

      (this.menuSrtrip.Items[2] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Show", null, this.OnMenuClick));
      (this.menuSrtrip.Items[2] as ToolStripMenuItem).DropDown.Items.Add(new ToolStripMenuItem("Hide", null, this.OnMenuClick));

      this.listBox1.Parent = this;
      this.listBox1.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
      this.listBox1.Bounds = new System.Drawing.Rectangle(10, this.menuSrtrip.Height + 10, this.ClientSize.Width - 20, this.ClientSize.Height - this.menuSrtrip.Height + - 20);

      this.MainMenuStrip = this.menuSrtrip;
      this.Text = "MenuStrip example";
    }

    void OnMenuClick(object sender, EventArgs e) {
      this.listBox1.Items.Add(string.Format("{0}/{1} clicked", (sender as ToolStripMenuItem).OwnerItem.Text, (sender as ToolStripMenuItem).Text));
      this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
    }

    MenuStrip menuSrtrip = new MenuStrip();
    ListBox listBox1 = new ListBox();
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author