MenuStrip Control in C#

In this article, we will learn how to use MenuStrip in windows form in C#.

Let's create windows form.

Step 1
In Visual Studio select "File" = > "New" = > "Project..." then select C# Windows Forms Application then click Ok.

create windows form

Step 2
Let's create a MenuStrip then you go "View" = > select "ToolBox" (Crtl+Alt+X) = > then select "All Windows Forms" = > Choose " MenuStrip".

 MenuStrip
  1. First of all, we'll know about the "MenuStrip".

    "MenuStrip" displays application commands and options grouped by functionality. We'll discuss the properties and functions and events of MenuStrip write down.

    Write the runtime code on "Form_Load" events.

    "BackColor" changes the background color of menuStrip control.

    menuStrip1.BackColor = Color.AliceBlue;
    

    "Click" when user mouse click on menuStrip control.

    menuStrip1.Click+=new EventHandler(menuStrip1_Click);
    private void menuStrip1_Click(object sender, EventArgs e)
    {
        MessageBox.Show("You click on menuStrip");
    }
    

    "Cursor" when user mouse hover on menuStrip control then cursor style changed.

    menuStrip1.Cursor = Cursors.Hand;
    

    "Dispose()" released all resources used by the System.ComponentModel.Component.

    menuStrip1.Dispose();
    

    "DoubleClick" when user mouse double click on menuStrip control.

    menuStrip1.DoubleClick+=new EventHandler(menuStrip1_DoubleClick);
    private void menuStrip1_DoubleClick(object sender, EventArgs e)
    {
        MessageBox.Show("You double click on menuStrip1");
    }
    

    "Enabled" changes the menuStrip control are enable or not.

    menuStrip1.Enabled = true;
    

    "Focus()" focusing the menuStrip control.

    menuStrip1.Focus();
    

    "Font" changes the style of the text of menuStrip control.

    menuStrip1.Font = new System.Drawing.Font("arial", 14.0F, FontStyle.Bold);
    

    "ForeColor" changes the font color of menuStrip control.

    menuStrip1.ForeColor = Color.AliceBlue;
    

    If you want to adding Items on menustrip then you create a Item then you go "arrow icon"(that are present menustrip box) = > Click "Edit items"

    adding menustrip items

    OR

    You can go menustrip properties and select the "Items".

     adding menustriip items with properties

    Then you see "Items Collection Editor".

    items collection editor

    Then you choose what items = > Click "Add" option.

    choosing items

    If you want to change any items text then select item menustrip items = > Choose right side page "Text" properties and change the "Text" = > finally Click the "OK" button.

    items text change

    When you want to a specific item click option.

    menuStrip1.Items[0].Click+=new EventHandler(FileItem_Click);
    private void FileItem_Click(object sender, EventArgs e)
    {
        MessageBox.Show("You click First Item");
    }
    

    "Height" set the height of menuStrip control.

    menuStrip1.Height=400;
    

    "Hide()" hides the menuStrip control.

    menuStrip1.Hide();
    

    "MouseDoubleClick" when user double click on menuStrip items mouse.

    menuStrip1.Items[0].DoubleClick += new EventHandler(FileItem_DoubleClick);
    private void FileItem_DoubleClick(object sender, EventArgs e)
    {
        MessageBox.Show("you double click first item");
    }
    

    "MouseEnter" when mouse pointer enters on menuStrip items.

    menuStrip1.Items[0].MouseEnter+=new EventHandler(FileItem_MouseEnter);
    private void FileItem_MouseEnter(object sender, EventArgs e)
    {
        MessageBox.Show("your mouse enter on menustrip items");
    }
    

    "MouseHover" when mouse pointer rests/hover on menuStrip items.

    menuStrip1.Items[0].MouseHover += new EventHandler(FileItem_MouseEnter);
    private void FileItem_MouseEnter(object sender, EventArgs e)
    {
        MessageBox.Show("your mouse hover the menustrip items");
    }
    

    "MouseLeave" when mouse pointer leaves the menustrip items.

    menuStrip1.Items[0].MouseLeave += new EventHandler(FileItem_MouseLeave);
    private void FileItem_MouseLeave(object sender, EventArgs e)
    {
        MessageBox.Show("your mouse leave the menustrip items");
    }
    

    "MouseMove" when mouse pointer moves the menustrip items.

    menuStrip1.Items[0].MouseMove+=new MouseEventHandler(FileItem_MouseMove);
    private void FileItem_MouseMove(object sender, MouseEventArgs e)
    {
        MessageBox.Show("your mouse pointer moves the menustrip items");
    }
    

    "ResetText()" resets all properties of menuStrip to its default values.

    menuStrip1.ResetText();
    

    "Show()" displays the menuStrip control.

    menuStrip1.Show();
    

    "Text" gets or sets text on menustrip items.

    menuStrip1.Items[2].Text = "SecondItem";
    

    "TextDirection" gets or sets direction the menustrip items.

    menuStrip1.TextDirection = ToolStripTextDirection.Horizontal;
    

    "Visible" gets or sets the menuStrip control visible or not.

    menuStrip1.Visible = true;
    


Write a comment


Name Email Message