在Winform窗体中来回切换,可以通过以下几种方式实现:
Show()
和Hide()
方法来显示和隐藏窗体。例如,创建两个窗体对象form1和form2,在form1中点击按钮切换到form2,同时隐藏form1,在form2中点击按钮切换到form1,同时隐藏form2。// Form1.cs
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
this.Hide();
}
// Form2.cs
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Show();
this.Hide();
}
// Form1.cs
private void button1_Click(object sender, EventArgs e)
{
tabControl1.SelectTab(0); // 切换到第一个页面
}
private void button2_Click(object sender, EventArgs e)
{
tabControl1.SelectTab(1); // 切换到第二个页面
}
// 或者
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = true; // 显示panel1
panel2.Visible = false; // 隐藏panel2
}
private void button2_Click(object sender, EventArgs e)
{
panel1.Visible = false; // 隐藏panel1
panel2.Visible = true; // 显示panel2
}
// Form1.cs (MDI主窗体)
private void button1_Click(object sender, EventArgs e)
{
ChildForm1 form1 = new ChildForm1();
form1.MdiParent = this; // 设置子窗体的父窗体为当前窗体
form1.Show();
}
private void button2_Click(object sender, EventArgs e)
{
ChildForm2 form2 = new ChildForm2();
form2.MdiParent = this; // 设置子窗体的父窗体为当前窗体
form2.Show();
}
// ChildForm1.cs (MDI子窗体)
private void button1_Click(object sender, EventArgs e)
{
this.Close(); // 关闭当前子窗体
}
以上是三种常见的实现方式,根据具体需求选择适合的方式来实现Winform窗体的来回切换。