如何避免重复的形式创作.NET Windows窗体? [英] How to avoid duplicate form creation in .NET Windows Forms?
本文介绍了如何避免重复的形式创作.NET Windows窗体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用.NET Windows窗体。我的MDI父窗体包含菜单。如果点击菜单的形式将被显示。到现在为止没有任何问题。
I am using .NET Windows Forms. My MDI parent form contains the menu. If click the menu the form will be displayed. Up to now no problem.
UserForm uf = new UserForm();
uf.Show();
uf.MdiParent = this;
如果我再次点击创建窗体的另一个副本的菜单。如何解决这个问题呢?
If I click the menu again another duplicate of the form is created. How to solve this issue?
推荐答案
最彻底的方法是简单地跟踪形式实例的生命周期。订阅的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();
}
}
这篇关于如何避免重复的形式创作.NET Windows窗体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文