C#在父窗体的容器中打开子窗体

//检查容器中是否已打开其他窗体

private void CloseForm()
{
	foreach(Control item in this.panel1.Controls)
	{
		if(item is Form)
		{
			Form form = (Form)item;
			form.Close();
		}
	}
}

嵌入子窗体

private void OpenForm(Form form)
{
	form.TopLevel = false;
	form.FormBorderStyle = FormBorderStyle.None;
	form.Parent = panel1;
	form.Show();
	//在主窗体大小变动时,子窗体大小可以随其变动
	form.Width = this.panel1.Width; 
	form.Height = this.panel1.Height;
}

//按钮事件调用方法嵌入窗体

private void btnPlan_Click(object sender, EventArgs e)
{
	CloseForm();
	OpenForm(new FrmPlan());
}
//或者直接清空panel所有控件
private void btnPlan_Click(object sender, EventArgs e)
{
	panel1.Controls.Clear();
	OpenForm(new FrmPlan());
}

你可能感兴趣的:(C#)