C # How to Implement the showdialog of a non-modal form (show)

Source: Internet
Author: User

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
  1. {
  2. Public mainform ()
  3. {
  4. Initializecomponent ();
  5. }
  6. Private void button#click (Object sender, eventargs E)
  7. {
  8. Forma forma = new forma ();
  9. Forma. showdialog ();
  10. }
  11. }
Related Article

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.