[Original] prevent child forms from being instantiated multiple times in the MDI form program-C # simple implementation of Singleton (Singleton Mode)

Source: Internet
Author: User

Recently I am working on a winform application.ProgramWhen the MDI form is used, a lot of trouble is encountered. One of them is how to solve the problem of not letting the child form be instantiated multiple times.

I have searched the internet for a long time and found many solutions. Here I will introduce some simple methods.

In fact, this is the singleton mode in the classic 23 design patterns. The following uses C # To implement it:

Suppose we have two winforms. The main form is mainform and the child form is childform.

Add a button1 in the main form to instantiate the child form.Code:

Private Static childform; // static variable to save the unique instance

Private void button#click (Object sender, system. eventargs E)
{
If (childform = NULL | childform. isdisposed)
{// If the first instantiation or instantiation form is disabled, it must be re-instantiated.
Childform = new childform ();
Childform. mdiparent = this;

Childform. Show (); // display

}
}

Bytes ----------------------------------------------------------------------------------------------------------

The above code has been run in vs2008 (C #), and my personal abilities are limited. I hope you can give me some advice!

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.