How to achieve window navigation in C#
To achieve switching between windows, the following methods can be used:
- Utilizing multiple forms in a Windows Forms Application: Add several forms to your project and, when needed, create a new form object and display it by clicking on a button or other triggering event. For example:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
this.Hide(); // 隐藏当前窗体
}
In a new window, you can return to the previous window by closing the current window. For example:
private void button2_Click(object sender, EventArgs e)
{
this.Close(); // 关闭当前窗体
}
- Using panel controls: Place multiple panels in a form, with each panel representing a different page. Switch between pages by displaying and hiding different panels. For example:
private void button1_Click(object sender, EventArgs e)
{
panel1.Visible = false;
panel2.Visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
panel2.Visible = false;
panel1.Visible = true;
}
- Utilizing Tab control: place a Tab control on a form, where each tab represents a different page. Switching between tabs allows for navigating between pages. For example:
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex == 0)
{
// 切换到第一个选项卡
}
else if (tabControl1.SelectedIndex == 1)
{
// 切换到第二个选项卡
}
}
These are three common methods for jumping between windows, you can choose a suitable method based on your specific needs to implement.