-6

我有两种形式:Form1 和 Form2。Form1 有 button1,Form2 有 button2。项目运行时,显示Form1,点击button1,显示Form2,隐藏form1。如果我点击 button2 那么 Form1 是可见的并且 Form2 是关闭的!

表格1:

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


    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form2 fo = new Form2();
        fo.Visible = true;
    }


}

表格2:

公共部分类 Form2 : Form { public Form2() { InitializeComponent(); }

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        f1.Visible = true;
        this.Close();
    }


}
4

2 回答 2

0
void Button1_Click(...)
{
   Form2 f2=new Form2(form1);
   f2.ShowDialog();
   form2.Hide();
}

void Button2_Click(...)
{
   this.Hide();
   form1.Show();
}
于 2013-06-10T11:54:15.237 回答
0

假设 Form1 是在 program.cs 中传递给 Application.Run() 的“启动表单”:

public partial class Form1 : Form
{

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();

        foreach (Form frm in Application.OpenForms)
        {
            if (frm is Form2)
            {
                frm.Show();
                return;
            }
        }

        Form2 f2 = new Form2();
        f2.Show();
    }

}

和:

public partial class Form2 : Form
{

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();

        foreach (Form frm in Application.OpenForms)
        {
            if (frm is Form1)
            {
                frm.Show();
                return;
            }
        }
    }

}
于 2013-06-10T16:24:10.153 回答