要在WinForm中实现多个窗口切换功能,可以使用以下几种方法:
Form.Show()
和Form.Hide()
方法来显示和隐藏窗体。private Form1 form1;
private Form2 form2;
private void btnSwitchToForm1_Click(object sender, EventArgs e)
{
if (form1 == null)
{
form1 = new Form1();
form1.FormClosed += (s, args) => form1 = null;
form1.Show();
}
else
{
form1.Show();
form1.WindowState = FormWindowState.Normal;
form1.BringToFront();
}
if (form2 != null)
form2.Hide();
}
private void btnSwitchToForm2_Click(object sender, EventArgs e)
{
if (form2 == null)
{
form2 = new Form2();
form2.FormClosed += (s, args) => form2 = null;
form2.Show();
}
else
{
form2.Show();
form2.WindowState = FormWindowState.Normal;
form2.BringToFront();
}
if (form1 != null)
form1.Hide();
}
TabControl.SelectedTab
属性来切换页。private void btnSwitchToTab1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage1;
}
private void btnSwitchToTab2_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage2;
}
Panel.Controls
属性来切换控件的可见性。private void btnSwitchToPanel1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
panel1.Controls.Add(new UserControl1());
}
private void btnSwitchToPanel2_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
panel1.Controls.Add(new UserControl2());
}
这些方法都可以实现多个窗口切换功能,你可以根据自己的需求选择其中一种来实现。