要在WinForm中實現(xiàn)多個窗口切換功能,可以使用以下幾種方法:
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());
}
這些方法都可以實現(xiàn)多個窗口切換功能,你可以根據(jù)自己的需求選擇其中一種來實現(xiàn)。