更新时间:2023-12-06 17:02:46
最彻底的方法是简单地跟踪形式实例的生命周期。订阅的FormClosed事件这样做。例如:
The cleanest way is to simply track the lifetime of the form instance. Do so by subscribing the FormClosed event. For example:
private UserForm userFormInstance;
private void showUserForm_Click(object sender, EventArgs e) {
if (userFormInstance != null) {
userFormInstance.WindowState = FormWindowState.Normal;
userFormInstance.Focus();
}
else {
userFormInstance = new UserForm();
userFormInstance.MdiParent = this;
userFormInstance.FormClosed += (o, ea) => userFormInstance = null;
userFormInstance.Show();
}
}