Winform Singleton form

Source: Internet
Author: User
Using system; using system. collections. generic; using system. windows. forms; using system. text; namespace common {/// <summary> /// Singleton mode of the form /// </Summary> /// <typeparam name = "T"> </typeparam> Public class formsingle <t> where T: form, new () {Private Static T form; Private Static ilist <t> List {Get; set;} public static t getform (T T1) {// check whether the form if (! Isexist (T1) {createnewform (T1);} return form ;} /// <summary> release object /// </Summary> /// <Param name = "OBJ"> </param> /// <Param name = "ARGs"> </param> Private Static void display (Object OBJ, formclosedeventargs ARGs) {form = NULL; List. remove (form) ;}/// <summary> Create a new form // </Summary> Private Static void createnewform (T T1) {form = T1; form. formclosed + = new formclosedeventhandler (Display); // subscribe to a form close event, release object} // <summary> // whether the form exists // </Summary> // <Param name = "T1"> </param> // /<returns> </returns> Private Static bool isexist (T T1) {If (list = NULL) {list = new list <t> (); list. add (T1); Return false;} // if the form has the same text, it is considered as the same form foreach (VAR t in list) {If (T. TEXT = t1.text) return true;} List. add (T1); Return false ;}}}
The call is as follows:
 
The constructor customer without parameters. addcustomer customer = Common. formsingle <customer. addcustomer>. getform (new customer. addcustomer (); customer. mdiparent = This; // MDI form customer. windowstate = formwindowstate. maximized; // maximize customer. show (); customer. activate (); constructor with parameters customer. addcustomer customer = Common. formsingle <customer. addcustomer>. getform (new customer. addcustomer (customerid); customer. mdiparent = This; customer. windowstate = formwindowstate. maximized; customer. show (); customer. activate ();

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.