C # simple MDI child form tutorial

Source: Internet
Author: User

1. Create an MDI form by referring to the built-in example
1. Add the parent form mainform and add a menu bar, new form1 form2 windows
2. Add a command to new
private int childformnumber = 0;
private void newtoolstripmenuitem_click (Object sender, eventargs E)
{< br> form childform = new form (); // create a subform
childform. mdiparent = This; // This sentence is very important
childform. TEXT = "window" + childformnumber ++; // form title
childform. show (); // show

}< br> try it now and press menu new to generate a subform. However, when all sub-forms are maximized, the following will be blocked at the beginning. How can we find them? This requires the Windows menu item (not necessarily windows or whatever).
3. Set the mdiwindowlistitem attribute of the Windows menu to true on the Internet, but I cannot find it in vs2008. You can set it like this to enable mainform. designer. CS: In the section " Code generated by the Windows Form Designer, add" this. menustrip1.mdiwindowlistitem = This. windowstoolstripmenuitem; "(you can change the menu name if it is different ). Now, after you press "new" in the menu, the new subform name is automatically added to windows, which is the same as word.

2. display the self-made form.
In fact, the above example is not very useful unless everyone makes up the board. Okay. Now, let's make a small improvement. Display the self-created form in the sub-form.
Create two forms form1 and form2 in the project, and add commands to the form1 and form2 menus respectively.
Private void form1toolstripmenuitem_click (Object sender, eventargs E)
{
Form1 form1 = new form1 ();
Form1.mdiparent = this;
Form1.show ();
}

Private void form2toolstripmenuitem_click (Object sender, eventargs E)
{
Form2 form2 = new form2 ();
Form2.mdiparent = this;
Form2.show ();
}
Click form1 and form2 to display the form1 and form2 forms in the project. However, this is useless (many tutorials on the internet end with this step), because after you click form1 twice, two identical form1 forms will be displayed in the subform, it is obviously useless in actual programming. For example, if I want to set several parameters in the form1 form in my project, it is enough for form1 to display one. Next we will make some improvements,
Private Static form1 form1;
Private void form1toolstripmenuitem_click (Object sender, eventargs E)
{
If (form1 = NULL | form1.isdisposed ){
Form1 = new form1 ();
Form1.mdiparent = this;
Form1.show ();}
Else {form1.activate ();}
}

Private Static form2 form2;
Private void form2toolstripmenuitem_click (Object sender, eventargs E)
{
If (form2 = NULL | form2.isdisposed)
{
Form2 = new form2 ();
Form2.mdiparent = this;
Form2.show ();
}
Else {form2.activate ();}
}
In this way, the form1 and form2 forms are instantiated only once. That is, when the form1 form is displayed, and then click form1, form1 will not be created again, but will only be displayed at the beginning of the above mentioned below, into an active form.

3. remark: the MDI container cannot use toolstripcontainer, but can be replaced by toolstrippanels.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.