在父窗体里创建一个public ChildFormCollection childForm
在子窗体的load函数里向父窗体的childForm.add(this);
在子窗体的close函数里向父窗体的childForm.remove(this);
在父窗体里检测childForm是否为空,或者直接把子窗体显示出来。
public class ChildFormCollection{
public add(){
Messgesbox.Show("呀,新的窗体,我要干点什么呢?");
}
public remove(){
Messgesbox.Show("呀,少了个窗体,我看看是不是都没有了,没有了的话我要禁用按钮了");
}
}
在子窗体的构造函数中传递变量。
那么麻烦~用计数器
//创建新窗口
private void OpenNew(object sender, EventArgs e) {
var f2 = new Form2 { MdiParent = this, };
f2.FormClosed += Form2Closed;
f2.Show();
if (++windows > 0) {
//……
}
}
private int windows = 0;
private void Form2Closed(object sender, EventArgs e) {
if (--windows <= 0) {
//……
}
}
用委托事件
在子窗体和父窗体之间进行调用