MDI forms and related issues, and questions about mdi forms
What is the MDI form? The multi-document form contains SDI, which is a single-document form. In general, we will set the main form of the software as the MDI form. The other forms that are called in the menu section of the main form are subforms of the main form.
(1) how to call a subform in the main form? Paste the Code. This code is of course written in the buttons or menu events in the main form, which is very simple.
Private void competition subject ToolStripMenuItem_Click (object sender, EventArgs e) {Form childForm = new Form (); childForm. mdiParent = this; childForm. text = "window"; childForm. windowState = FormWindowState. maximized; // The child form maximizes childForm in the parent form. show ();}
(2) sorting of child forms. If the system creates a new MDI parent form, there is actually a sample code in it, but it should be pasted out, child forms are arranged in layers, horizontally, vertically, and so on.
Private void CascadeToolStripMenuItem_Click (object sender, EventArgs e) {LayoutMdi (MdiLayout. cascade); // Cascade} private void TileVerticalToolStripMenuItem_Click (object sender, EventArgs e) {LayoutMdi (MdiLayout. tileVertical); // vertical tile} private void TileHorizontalToolStripMenuItem_Click (object sender, EventArgs e) {LayoutMdi (MdiLayout. tileHorizontal); // horizontal tile} // close all subforms private void CloseAllToolStripMenuItem_Click (object sender, EventArgs e) {foreach (Form childForm in mdichilform) {childForm. close ();}}
(3) another problem is that the same subform cannot be opened repeatedly.
/// <Summary> /// check whether the opened MDI window is opened. If yes, activate it, do not open it again // </summary> // <param name = "Frmname"> </param> private bool CheckFrm (Form cForm) {foreach (Form sonfrm in this. mdichil1_) {if (sonfrm. name = cForm. name) {return false ;}} return true;} private void ShowNewForm (object sender, EventArgs e) {Form childForm = new Form (); childForm. text = "window"; childForm. name = "StudentForm"; if (CheckFrm (childForm) {childForm. mdiParent = this; childForm. show (); childForm. activate ();} else {childForm. activate (); childForm. windowState = FormWindowState. normal ;}}
// For non-MDI forms, you can use the following method to detect private bool CheckOpen (Form cForm) {foreach (Form frm in Application. openForms) // traverses the opened window {if (frm. name = cForm. name) // if this window has opened {frm. activate (); // Activate the current form if (frm. windowState = FormWindowState. minimized) // if the current form has been Minimized {frm. windowState = FormWindowState. normal; // restore form} return true;} return false ;}
I am using the method, I feel pretty good
Put a splitContainer1 in the MDI parent window and set the parent component of the MDI child form to a panel of splitContainer1. This is suitable for the shortcut menu on the left. First, the previous Graph
In this way, the subform can be placed in pannel2. Code on
/// <Summary> /// menu athlete information /// </summary> /// <param name = "sender"> </param> /// <param name = "e"> </param> private void athlete information ToolStripMenuItem_Click (object sender, eventArgs e) {ShowMemberInfoFrm () ;}/// <summary> // display the athlete Information Management window /// </summary> private void ShowMemberInfoFrm () {this. splitContainer1.Panel2. controls. clear (); // This area directly kills the currently displayed child form, and then displays the new form MemberInfoFrm frm = new MemberInfoFrm (); frm. mdiParent = this; frm. parent = splitContainer1.Panel2; frm. show (); frm. dock = DockStyle. fill; // This is very useful, that is, the sub-form is full of the pannel on the right, and changes with the size of the pannel}
Here is a problem, whether it is the MDI form or the pannel that I use, you can use the form to repeatedly open the detection, that is, the method in (3) to check, and then activate it. However, the test proves that the function cannot be activated, and the overwritten form is not displayed at the beginning. Some friends say that the TopMost or TopLevel attribute is used. I tried it, but I still cannot. If you have any good solutions, please post the code. Thank you.