|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Walkthrough: Creating an MDI Form with Menu Merging and ToolStrip Controls
This walkthrough demonstrates how to use ToolStripPanel controls with an MDI form. The form also supports menu merging with child menus. The following tasks are illustrated in this walkthrough:
Creating a Windows Forms project.
Creating the main menu for your form. The actual name of the menu will vary.
Adding the ToolStripPanel control to the Toolbox.
Creating a child form.
Arranging ToolStripPanel controls by z-order.
When you are finished, you will have an MDI form that supports menu merging and movable ToolStrip controls.
To copy the code in this topic as a single listing, see How to: Create an MDI Form with Menu Merging and ToolStrip Controls.
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.
The first step is to create the project and set up the form.
To create the project
The parent MDI form contains the main menu. The main menu has one menu item named Window. With the Window menu item, you can create child forms. Menu items from child forms are merged into the main menu.
To create the Main menu
From the Toolbox, drag a MenuStrip control onto the form.
Select the MenuStrip control.
In the Properties window, set the value of the MdiWindowListItem property to ToolStripMenuItem1.
Add a subitem to the Window menu item, and then name the subitem New.
In the Properties window, click Events.
Double-click the Click event.
The Windows Forms Designer generates an event handler for the Click event.
Insert the following code into the event handler.
To add the ToolStripPanel control to the Toolbox
Open the Toolbox, and then click the All Windows Forms tab to show the available Windows Forms controls.
Right-click to open the shortcut menu, and select Choose Items.
In the Choose Toolbox Items dialog box, scroll down the Name column until you find ToolStripPanel.
Select the check box by ToolStripPanel, and then click OK.
The ToolStripPanel control appears in the Toolbox.
In this procedure, you will define a separate child form class that has its own MenuStrip control. The menu items for this form are merged with those of the parent form.
To define a child form
Add a new form named ChildForm to the project.
For more information, see How to: Add Windows Forms to a Project.
From the Toolbox, drag a MenuStrip control onto the child form.
Click the MenuStrip control's smart tag glyph (), and then select Edit Items.
In the Items Collection Editor dialog box, add a new ToolStripMenuItem named ChildMenuItem to the child menu.
For more information, see ToolStrip Items Collection Editor.
To test your form
Press F5 to compile and run your form.
Click the Window menu item to open the menu, and then click New.
A new child form is created in the form's MDI client area. The child form's menu is merged with the main menu.
Close the child form.
The child form's menu is removed from the main menu.
Click New several times.
To add ToolStrip controls to the MDI parent form
From the Toolbox, drag a ToolStripPanel control onto the form.
Select the ToolStripPanel control.
Repeat steps 1 through 4.
Dock the new ToolStripPanel control to the top of the form.
The ToolStripPanel control is docked underneath the main menu, but to the right of the first ToolStripPanel control. This step illustrates the importance of z-order in correctly positioning ToolStripPanel controls.
Repeat steps 1 through 4 for two more ToolStripPanel controls.
Dock the new ToolStripPanel controls to the right and bottom of the form.
The position of a docked ToolStripPanel control on your MDI form is determined by the control's position in the z-order. You can easily arrange the z-order of your controls in the Document Outline window.
To arrange ToolStripPanel controls by Z-order
In the View menu, click Other Windows, and then click Document Outline.
The arrangement of your ToolStripPanel controls from the previous procedure is nonstandard. This is because the z-order is not correct. Use the Document Outline window to change the z-order of the controls.
In the Document Outline window, select ToolStripPanel4.
Click the down-arrow button repeatedly, until ToolStripPanel4 is at the bottom of the list.
The ToolStripPanel4 control is docked to the bottom of the form, underneath the other controls.
Click the down-arrow button one time to position the control third in the list.
The ToolStripPanel2 control is docked to the top of the form, underneath the main menu and above the other controls.
Select various controls in the Document Outline window and move them to different positions in the z-order. Note the effect of the z-order on the placement of docked controls. Use CTRL-Z or Undo on the Edit menu to undo your changes.
Created a form with an automatically populated standard menu. For more information, see Walkthrough: Providing Standard Menu Items to a Form.
Give your ToolStrip controls a professional appearance. For more information, see How to: Set the ToolStrip Renderer for an Application.