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 ();