70-526 MCTS


Create a MenuStrip component on a Windows Form

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.


Inserting standard items

Once you have added a menu to the Form, you are free to start adding items to the MenuStrip.

Before you add any items, you may want to add standard items that you find in most windows applications. To insert a set of standard items by clicking on the Smart Tag icon and choosing Insert Standard Items.

The menus that are added are shown below:


Adding any item

To add a menu item to the MenuStrip


Manually adding items


MSDN references

  • Walkthrough: Providing Standard Menu Items to a Form


    << Previous Contents Next >>

    © Publicjoe, 2007