Winform, especially the management system, usually needs to open only one instance in the same window. That is, when this window is not created, it is created and displayed. When the window already exists, it is placed on the top and activated.
From the perspective of design patterns, it should be regarded as Singleton.
For the implementation of single-piece mode, you can refer to the http://terrylee.cnblogs.com/archive/2005/12/09/293509.html
The introduction is detailed.
The following is a single-piece window mode.
A single piece of window can also be written by a few words in the Form classCodeSimple implementation:
Public partial class form1: Form <br/>{< br/> Private Static form1 instance = NULL; <br/> Public static form1 instance <br/> {<br/> Get <br/> {<br/> If (instance = NULL) <br/>{< br/> instance = new form1 (); <br/>}< br/> return instance; <br/>}< br/> private form1 () <br/>{< br/> initializecomponent (); <br/>}< br/> private void form=formclosed (Object sender, formclosedeventargs e) <br/>{< br/> instance = NULL; <br/>}< br/>}
1.Change the constructor to private
2.Add static attribute instance
3.In the window close event, set instance to NULL;
In this way, you can generate a unique window instance through form1.instance.
However, there are many shortcomings:
1.When there are many child forms in the primary MDI form, similar repeated code must be added to these child forms.
2. If a subform calls another subform and does not want to follow the same window opened in the main form (that is, create another window), it cannot be implemented. (In fact, changing the constructor to public can also be implemented, but it is not a strict single piece)
Therefore, it is best to create a single form by writing a separate class.
Obviously, generic implementation is used:
/// <Summary> <br/> // you can use a wildcard to create a single form instance. <br/> /// </Summary> <br/> // <typeparam name = "T"> form class </typeparam> <br/> Public static class Singleton <t> where T: form, new () <br/>{< br/> Private Static t instance = NULL; <br/> Private Static readonly object lockhelper = new object (); <br/> /// <summary> <br/> // obtain the unique instance of the form <br/> /// </Summary> <br/> Public static t instance <br/> {<br/> Get <br/> {<br/> If (instance = NULL) <br/>{< br/> lock (lockhelper) <br/>{< br/> If (instance = NULL) <br/>{< br/> instance = new T (); <br/> instance. formclosed + = new formclosedeventhandler (destroyform); <br/>}< br/> return instance; <br/>}< br/> /// <summary> <br/> // leave the instance blank when the form is closed <br/>/ /// </Summary> <br/> /// <Param name = "sender"> </param> <br/> /// <Param name = "E"> </param> <br/> Private Static void destroyform (Object sender, formclosedeventargs e) <br/>{< br/> instance = NULL; <br/>}</P> <p>}
Where T: form, new () indicates that T must be a form and has a non-argument constructor.
Lockhelper is designed for multi-threaded security. Refer to the above link.
When a new form class is generated, instance. formclosed + = new formclosedeventhandler (destroyform); register the delegate implementation. When the form is closed, set the instance to null.
In this way, any common form class can be called to generate a single form.
When calling, as longSingleton <childform>. Instance
You can obtain the unique childform form instance generated by this class.
Of course, you can also write a generic function in the primary MDI form to facilitate the display of child forms:
// <summary> <br/> // /display form, and placed at the frontend <br/> /// </Summary> <br/> /// <typeparam name = "T"> </typeparam> <br/> private void showform <t> () <br/> where T: form, new () <br/> {<br/> T f = Singleton <t>. instance; <br/> F. mdiparent = This; <br/> F. show (); <br/> If (F. windowstate = formwindowstate. minimized) <br/> F. windowstate = formwindowstate. normal; <br/> F. bringtofront (); <br/>}