Home > DeveloperSection > Articles > MenuStrip Control in C#.Net

MenuStrip Control in C#.Net


C# C# 
Ratings:
1 Comment(s)
 25699  View(s)
Rate this:

MenuStrip Control in C#.Net


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.

How to use MenuStrip Control

Drag and drop MenuStrip control from toolbox on the window Form.

MenuStrip Control in C#.Net

MenuStrip Control in C#.Net

For creating menu simply add menu item in the shown textbox. Insert text in the MenuStrip which you want to display as menu.

MenuStrip Control in C#.Net

New and open is the sub Item of File.

MenuStrip Control in C#.Net

MenuStrip Control in C#.Net

Create event handlers for the above menu items as New, Open, Save, Exit on the MenuStrip and selecting submenu items  then double click on them.

    private void newToolStripMenuItem_Click(object sender, EventArgs e)

    {           

    }

    private void openToolStripMenuItem_Click(object sender, EventArgs e)

    {

    }

    private void saveToolStripMenuItem_Click_1(object sender, EventArgs e)

    {

    }

    private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)

    {          

    }

Change the Form to an MDI Form.

private void frmMenuStrip_Load(object sender, EventArgs e)

{

            this.IsMdiContainer = true;

}

Write code on the Click event of the New submenu item.

private void newToolStripMenuItem_Click(object sender, EventArgs e)

{

            Form fr = new Form();

            fr.MdiParent = this;

            fr.Text = "New";

}

Run The Project

MenuStrip Control in C#.Net

New child Form will open when New menu item clicked.

MenuStrip Control in C#.Net

Write code on the Click event of the Open  submenu item.

private void openToolStripMenuItem_Click(object sender, EventArgs e)

{

            OpenFileDialog fld = new OpenFileDialog();

            fld.ShowDialog();

}

Open dialog will open when Open menu item  clicked.

Write code on the Click event of the Save submenu item.

private void saveToolStripMenuItem_Click_1(object sender, EventArgs e)

{

            saveFileDialog1.ShowDialog();

}

Save dialog will open when Save menu item clicked.

Write code on the Click event of the Exit  submenu item.

private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)

{

            this.Close();

}

Form will close when Exit menu item clicked.

MenuStrip properties


GripStyle:  Gets or sets the visibility of the grip used to reposition the control. 

GripMargin : Gets or sets the space around the ToolStrip move handle.

AutoSize: Gets or sets a value indicating whether the control is automatically resized to display its entire contents.

AllowMerge:  Gets or sets a value indicating whether multiple MenuStrip can be combined.

 


Small suggestion and modification in this article.

By mohan kumar on   5 years ago
This code section doesn't work and created a new child form named "New", untill i include this line..

fr.show();

private void newToolStripMenuItem_Click(object sender, EventArgs e)

{

     Form fr = new Form();

     fr.MdiParent = this;

     fr.Text = "New";
}

So,the above should be rewritten as

private void newToolStripMenuItem_Click(object sender, EventArgs e)

{

     Form fr = new Form();

     fr.MdiParent = this;

     fr.Text = "New";

     fr.show();
}

However,i welcome any new suggestions from you guys.. :) ;)

Don't want to miss updates? Please click the below button!

Follow MindStick