如何在表单之间切换而不创建新的表单实例? [英] How to switch between forms without creating new instance of forms?

查看:99
本文介绍了如何在表单之间切换而不创建新的表单实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有四个表单form1 form2 form3 form4。每个表单都有两个按钮,即在表单之间切换的下一个和上一个按钮。我的问题是如何在表单之间切换而不创建新的表单实例?下面是我的代码

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

在Form1中:

    public Form1()
   {
       InitializeComponents();
   }

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

在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();
    }      

在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();
    }      

在Form4中:

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

在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

推荐答案

由于要顺序访问表单,因此请确保使用Show方法,该方法将所有者分配给所创建的Form,并在创建后将其分配给类级别的变量。

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();
    }
}

这篇关于如何在表单之间切换而不创建新的表单实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆