Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
180 views
in Technique[技术] by (71.8m points)

c# - How to switch between forms without creating new instance of forms?

in my application i have four forms form1 form2 form3 form4 .and each form have two buttons i.e next and previous buttons to switch between forms .and my question is how can i Switch between forms without creating new instance of forms? below is my code

In Form1:

    public Form1()
   {
       InitializeComponents();
   }

    private void Next_Click(object sender, EventArgs e)
    {
      this.Hide()
       Form2  form2 = new Form2();
       form2.Show();
    }      

In Form2:

    public Form2()
   {
       InitializeComponents();
   }
    private void Previous_Click(object sender, EventArgs e)
    {
       this.Hide();
       Form1 form1 = new Form1();
       form1.Show();
    }

    private void Next_Click(object sender, EventArgs e)
    {
         this.Hide();
       Form3 form3 = new Form3();
       form3.Show();
    }      

In Form3:

    public Form3()
   {
       InitializeComponents();
   }
    private void Previous_Click(object sender, EventArgs e)
    {
        this.Hide();
       Form2 form2 = new Form2();
       form2.Show();
    }

    private void Next_Click(object sender, EventArgs e)
    {
         this.Hide();
       Form4 form4 = new Form4();
       form4.Show();
    }      

In Form4:

    public Form4()
   {
       InitializeComponents();
   }
    private void Previous_Click(object sender, EventArgs e)
    {
         this.Hide();
       Form3 form3 = new Form3();
       form3.Show();
    }

In Main:

static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        Application.Run(new Form1());


    }

In above code i am creating new instances of forms every time..,How can i Avoid this and How can i Switch between forms without creating new instances of forms.... please help me

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since you are accessing your forms sequentially just make sure that you use the Show Method that assigns the owner to the created Form and assign it to a class level variable after you create it. Something like this should work for you.

Form1

public partial class Form1 : Form
{
    Form2 frm2;
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (frm2 == null)
        {
            frm2 = new Form2();   //Create form if not created
            frm2.FormClosed += frm2_FormClosed;  //Add eventhandler to cleanup after form closes
         }

        frm2.Show(this);  //Show Form assigning this form as the forms owner
        Hide();
    }

    void frm2_FormClosed(object sender, FormClosedEventArgs e)
    {
        frm2 = null;  //If form is closed make sure reference is set to null
        Show();
    }
}

Form2

public partial class Form2 : Form
{
    Form3 frm3;
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Owner.Show();  //Show the previous form
        Hide();

    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (frm3 == null)
        {
            frm3 = new Form3();
            frm3.FormClosed += frm3_FormClosed;
        }

        frm3.Show(this);
        Hide();
    }

    void frm3_FormClosed(object sender, FormClosedEventArgs e)
    {
        frm3 = null;
        Show();
    }
}

Form3

public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Owner.Show();
        Hide();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...