The basic element of the Multi-Document Interface (MDI) application is the MDI child form, because they are the center of user interaction. In the following process, an MDI child form that displays the RichTextBox Control is created, which is similar to most word processing applications. Replacing the RichTextBox Control with other controls, such as the DataGrid control or hybrid control, allows you to create various possible MDI subwindows (and further expand to the MDI application ). Create an MDI subform The create menu structure contains the top-level menu item "file" and "window", and the menu item "new" and "closed" MDI parent forms. For more information about creating an MDI parent form, see create an MDI parent form. In the drop-down list at the top of the "properties" window, select the menu item corresponding to the "window (& W)" menu item, and set the mdilist attribute to true. This enables the window menu to maintain the list of opened MDI child windows (there is a check mark next to the Active Child Window ). In Solution Explorer, right-click the project, point to "add", and select "Add new project ". This form is used as a template for the MDI subform. Note that the MDI child form created in this step is a standard Windows form. Therefore, it has the opacity attribute, which allows you to control the transparency of the form. However, the opacity attribute is used for top-level windows. Do not use it with the MDI child form at the same time, otherwise it may cause rendering problems. In the Add new project dialog box, select Windows Forms (in Visual Basic or Visual C #) or Windows Forms Application (. in Visual C ++ ). In the Name box, name the form form2. Click Open to add the form to the project. Open the windows Form Designer, where form2 is displayed. Drag the RichTextBox Control from the toolbox to the form. In the "properties" window, set the anchor attribute to "Top, left", and the dock attribute to "fill ". This causes the RichTextBox Control to completely fill the area of the Child MDI form even if the size of the child form is adjusted. Create a click event handler for the new menu item. For more information about creating an event handler, see create an event handler on Windows Forms designer. Insert code similar to the following code to create an MDI subform when you click the new menu item (in the following example, the event handler processes the click event of menuitem2. Note that your "new" menu item may not be menuitem2, depending on the application structure .). 'Visual basic Protected sub mdichildnew_click (byval sender as system. Object, byval e as system. eventargs) handles menuitem2.click Dim newmdichild as new form2 () 'Set the parent form of the Child Window. Newmdichild. mdiparent = me 'Display the new form. Newmdichild. Show () End sub // C # Protected void mdichildnew_click (Object sender, system. eventargs e ){ Form2 newmdichild = new form2 (); // Set the parent form of the Child Window. Newmdichild. mdiparent = this; // Display the new form. Newmdichild. Show (); } // C ++ PRIVATE: System: void menuitem2_click (System: object * sender, System: eventargs * E) { Form2 * newmdichild = new form2 (); // Set the parent form of the Child Window. Newmdichild-> mdiparent = this; // Display the new form. Newmdichild-> show (); } In Visual C ++, add the following # include command on form1.h: // C ++ # Include "form2.h" Press F5 to run the application. Note that you can create a new MDI subform by selecting "new" from the "file" menu. This subform is tracked in the "window" menu. Note that if the MDI sub-form has a mainmenu component (its menu structure is usually a menu structure containing menu items ), the child form is opened in an MDI parent form that has the mainmenu component (the menu structure usually includes the menu structure of the menu item, after the mergetype attribute is set (as an option, you can also set the mergeorder attribute), the two menu items are automatically merged. Set the mergetype attribute of the two mainmenu components and all menu items of the child form to mergeitems. In addition, set the mergeorder attribute so that the menu items of these two menus are displayed in the desired order. For more information about how to use the mergeorder attribute to determine the menu item location, see merging menu items programmatically. Note: When an MDI parent form is closed, each MDI child form triggers a closing event and then the closing event of the MDI parent form. However, canceling the closing event of the MDI child form does not affect the closing event of the MDI parent form, because the canceleventargs parameter of the closing event of the MDI parent form is now set to true. By setting the canceleventargs parameter to false, you can force the MDI parent form and all MDI child forms to close. |