articles

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

MenuStrip Control in C#.Net

MenuStrip Control in C#.Net

Anonymous User 60824 24-Jan-2011

MenuStrip Control in C#.Net

Where 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 the toolbox on the window Form.

MenuStrip Control in C#.Net

MenuStrip Control in C#.Net

For creating a menu simply add a menu item in the shown textbox. Insert text in the MenuStrip which you want to display as the 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();
}

The form will close when the 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.

 


c# c# 
Updated 23-Sep-2020
I am a content writter !

Leave Comment

Comments

Liked By