本文介紹了如何避免在 .NET Windows Forms 中創建重復的表單?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在使用 .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 Forms 中創建重復的表單?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!