Create a MenuStrip component on a Windows Form

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
70-505 Study Guide
70-526 Study Guide

1. Forms Controls

2. Integrating Data
3. Printing/Reporting
4. Enhancing Usability
5. Asynchronous Prog.
6. Forms Controls
7. Configure/Deploy

edit

Adding a MenuStrip to a Form is a relatively straight forward process. As with all components in .NET there are usually two methods of adding them to your application. Either graphically via the IDE, or manually by adding code.

  • To add a menu via the IDE, simply select the MenuStrip control from the Toolbox and click anywhere on the Form. Alternatively, double-click the MenuStrip control in the Toolbox. Your form will now look like this.


  • To add a menu via code takes a little more work than simply droping a control onto a Form. First we add a MenuStrip to the Form.
    private System.Windows.Forms.MenuStrip menuStrip1;

    Then in the constructor of the Form, after the call to InitializeComponent, initialise the instance of the MenuStrip.

    menuStrip1 = new System.Windows.Forms.MenuStrip();

    Also in the constructor, the properties of the MenuStrip need to be set up.

    menuStrip1.Location = new System.Drawing.Point(0, 0);
    menuStrip1.Name = "menuStrip1";
    menuStrip1.Size = new System.Drawing.Size(292, 24);
    menuStrip1.TabIndex = 0;
    menuStrip1.Text = "menuStrip1";

    Finally, also in the constructor, the MenuStrip is added to the Form's Controls collection.

    this.Controls.Add(this.menuStrip1);

    Running the application, the Form looks like this.

    Image:menuBasic.jpg

Today's Deals: Electronics

Personal tools