using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp6 { public partial class MainForm : Form { public MainForm() { InitializeComponent(); } //private void SwitchToForm() where T : Form, new() //{ // T form = new T(); // // 处理窗体关闭事件 // form.FormClosed += (s, args) => // { // this.Show(); // 显示主窗体 // this.Activate(); // 激活主窗体 // }; // form.Show(); // this.Hide(); //} private void SwitchToForm() where T : Form, new() { using (T form = new T()) { this.Hide(); form.ShowDialog(); this.Show(); this.Activate(); } } private void button1_Click(object sender, EventArgs e) { this.SwitchToForm(); } private void button2_Click(object sender, EventArgs e) { this.SwitchToForm(); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { // 退出应用程序 Application.Exit(); } private void button3_Click(object sender, EventArgs e) { this.SwitchToForm(); } } }