After the mode form (showdialog) is opened, no operation can be performed on the following main form.
In this blog, we want to implement non-modal forms (show) to achieve some effects of the mode form (the main form is closed, and the sub form is also closed. Only one subform can be opened .)
At the same time, some features of the non-modal form are retained (both the main form and sub-form can be operated, such as text writing ).
Practical Use:
1. For example, if you want to write existing content in the main form, use the subform to display the existing content (generally there are many information, and you cannot use a space in the main form to display the content completely) for reference.
2. Click the information in the subform and display it in the main form.
And so on.
The Code is as follows:
Forma ---- main form
Formb ---- subform
Using system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. text; <br/> using system. windows. forms; <br/> namespace basicwindowsapplication <br/>{< br/> Public partial class forma: Form <br/>{< br/> formb = NULL; <br/> Public forma () <br/>{< br/> initializecomponent (); <br/>}< br/> private void B Utton1_click (Object sender, eventargs e) <br/>{< br/> If (formb = NULL) <br/>{< br/> formb = new formb (this); // this step is very important <br/> formb. closefrm + = new eventhandler (frma_closefrm); <br/> formb. show (); <br/>}< br/> else <br/> {<br/> formb. topmost = true; <br/>}</P> <p >}< br/> // <summary> <br/> // form close event <br/> /// </Summary> <br/> /// <Param name = "sender"> </param> <br/> /// <Param name = "E"> </ para M> <br/> Public void frma_closefrm (Object sender, eventargs e) <br/>{< br/> formb = NULL; <br/>}< br/> /// <summary> <br/> // form close event <br/> /// </Summary> <br/> /// <Param name = "sender"> </param> <br/> /// <Param name = "E"> </param> <br/> private void frma_formclosed (Object sender, formclosedeventargs <br/> E) <br/>{< br/> If (formb! = NULL) <br/>{< br/> formb. Dispose (); <br/>}< br/>}
Using system; <br/> using system. collections. generic; <br/> using system. componentmodel; <br/> using system. data; <br/> using system. drawing; <br/> using system. text; <br/> using system. windows. forms; <br/> namespace basicwindowsapplication <br/>{< br/> Public partial class formb: Form <br/>{< br/> forma; <br/> public event eventhandler closefrm; <br/> Public formb (Forma FRM) <br/>{< br/> forma = FRM; <br/> initializecomponent (); <br/> This. formclosed + = new <br/> formclosedeventhandler (frmb_formclosed ); <br/>}< br/> /// <summary> <br/> // form close event <br/> /// </Summary> <br/> /// <Param name = "sender"> </param> <br/> /// <Param name = "E"> </param> <br/> void frmb_formclosed (Object sender, formclosedeventargs e) <br/>{< br/> If (closefrm! = NULL) <br/>{< br/> closefrm (sender, e); <br/>}< br/>}
In order to display the main form (forma), The mainform (formb) is also disabled.
Public partial class mainform: Form
- {
- Public mainform ()
- {
- Initializecomponent ();
- }
- Private void button#click (Object sender, eventargs E)
- {
- Forma forma = new forma ();
- Forma. showdialog ();
- }
- }