C # WinForm Base click multiple button to generate only one form

Source: Internet
Author: User
Tzu Chi Learning, technology is willing to learn. Good heart to create a lot of Masarica, after the shade. I write the experience today, I would like to see the text to enlighten.

1, the wrong code Form1

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;

Namespace WindowsFormsApplication4
{public
    partial class Form1:form
    {public
        Form1 ()
        {
            InitializeComponent ();
        }

        private void Button1_Click (object sender, EventArgs e)
        {
            Form2 frm2 = new Form2 ();
            Frm2. Show ();}}



Form2

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;

Namespace WindowsFormsApplication4
{public
    partial class Form2:form
    {public
        Form2 ()
        {
            InitializeComponent ();}}



Effect



2, improve the code

Form1

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;

Namespace WindowsFormsApplication4
{public
    partial class Form1:form
    {public
        Form1 ()
        {
            InitializeComponent ();
        }
        
        private void Button1_Click (object sender, EventArgs e)
        {
  
            Form2 frm2 = Form2.form2single ();           
            Frm2. Show ();}}




Form2

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;

Namespace WindowsFormsApplication4
{public
    partial class Form2:form
    {
        private Form2 ()
        {
            InitializeComponent ();
        }
        private static Form2 Form2 = null;
        public static Form2 Form2single ()
        {
            if (form2==null)
            {
                Form2 = new Form2 ();
            }
            return Form2;
        }
    }


The right effect



2017-11-29

This is the design pattern of a single case pattern, design patterns belong to the realm of Zen, beginners to this will be good, do not do too high requirements. You can buy a book and learn it.



Thanksgiving has helped the heart of the less park people.
C # is excellent and worth learning. WINFORM,WPF can be concerned about, broaden their horizons. The
Visual Studio IDE is very handy, recommended.
Note: This article is born of self-study notes, the quality of the lower, so think twice before you do. Novice to this, can not be copied, should first study its image number, to be able to adapt when the natural jump out of deep pits.

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.