父窗体中加入一个Panel,然后将要弹出的窗体作为Panel的控件显示到Panel上
以下的代码中,点击Button1可以实现把form2嵌入到form1中的panel1中,而点击Button2先清除panel1中可能有的控件,然后将form3嵌入panel1中。但怎样才能实现模拟点击关闭panel1中的嵌入的窗体,而不是用panel1.Controls.Clear();这样仅仅是清除掉嵌入的窗体。应该如何写代码呢?谢谢!
private void button1_Click(object sender, EventArgs e) //把form2嵌入form1中的panel1
{
Form2 aa = new Form2();
//aa.FormBorderStyle = FormBorderStyle.None;
aa.TopLevel = false;
this.panel1.Controls.Add(aa);
aa.Show();
}
private void button2_Click(object sender, EventArgs e)
{
this.panel1.Controls.Clear();
Form3 aa2 = new Form3();
//aa2.FormBorderStyle = FormBorderStyle.None;
aa2.TopLevel = false;
this.panel1.Controls.Add(aa2);
aa2.Show();
}
------解决方案-------------------- foreach (Control ctrl in this.panel1.Controls)
{
if (ctrl is Form)
{
ctrl.Dispose();
}
}
------解决方案--------------------
void button1_Click(object sender, EventArgs e) //把form2嵌入form1中的panel1
{
this.panel1.Controls.Clear();
Form2 aa = new Form2();
//aa.FormBorderStyle = FormBorderStyle.None;
aa.TopLevel = false;
this.panel1.Controls.Add(aa);
aa.Show();