using System; using System.Windows.Forms; namespace Panel内嵌入子窗体 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 嵌入窗体通用方法 /// </summary> /// <param name="subForm"></param> private void OpenSubFrm(Form subForm) { //首先关闭Panel内的所有form控件 foreach (Control item in this.panel1.Controls) { if (item is Form) { ((Form)item).Close(); } } subForm.TopLevel = false;//将子窗体设置为非顶级控件 subForm.Parent = this.panel1;//将Panel1设置为子窗体的父容器 subForm.FormBorderStyle = FormBorderStyle.None;//将子窗体的边框样式设置为无 subForm.Dock = DockStyle.Fill;//子窗体的dock属性设置为填充 subForm.Show(); } private void button1_Click(object sender, EventArgs e) { OpenSubFrm(new Form2()); } private void button2_Click(object sender, EventArgs e) { OpenSubFrm(new Form3()); } } }