winfrom中,父窗体中只允许显示一个子窗体的代码怎么写?
来源:百度知道 编辑:UC知道 时间:2024/05/30 05:59:18
private void ChangeChildren(Form frm)
{
//判断是否是第一次显示窗体
if (this.MdiChildren.Length == 1)
{
frm.Show();
return;
}
foreach (Form children in this.MdiChildren)
{
if (frm == children)
{
//如果是现在就存在的子窗体就激活它
children.Activate();
break;
}
else
{
//如果窗体不一样就关闭了,在显示现在所需要的
children.Close();
frm.Show();
break;
}
}
}
是这样调用的
Form f = new Form();
f.MdiParent = this;
f.Text = "1";
ChangeChildren(f);
用下面的函数打开子窗体就行了。
private void addChild(Form form)
{