Among the MDI form programs are prevented from instantification - the C # implementation of the Singleton

xiaoxiao2021-03-19  220

First of all, we have 2 Windows Form, the main form called Form1, the child form called ChildForm. Add a menu in the main form, use to instantiate the subform, we write down the code in Form1.cs:

Private static childform childform; // static variable, save unique instance

Private void Menuitem2_click (Object Sender, System.Eventargs E)

{

Childform ChildForm = getChildForm (); // Get a child form object

ChildForm.show (); // Display

}

Private child form getChildForm ()

{

// The order cannot be wrong, the previous condition is used to determine if it is the first time, the latter condition is used to determine if the child form is turned off.

IF (ChildForm == Null || CHildForm.Indisposed)

{// The first instantiation is also, it is closed, all re-instantiate

ChildForm = new childform ();

ChildForm.mdiparent = this;

}

Return CHildForm;

}

转载请注明原文地址:https://www.9cbs.com/read-130248.html

New Post(0)