我正在編寫一個應用程序,它使用5個簡單窗體的嚮導式系列。第一種形式NewProfile是從主應用程序MainForm的菜單項打開的,因此是MainForm的子窗體。第二種形式TwoProfile從NewProfile上的按鈕打開。第三種形式ThreeProfile是從TwoProfile上的一個按鈕打開的,以此類推爲所有5種形式。這裏是序列: MainForm - > NewProfile < - > TwoProfile < - > ThreeProfile < - > FourProfile < - > FiveProfile。我的問題是,當任何窗體(NewProfile,TwoProfile,ThreeProfile,FourProfile或FiveProfile)打開時,我不希望用戶能夠創建NewProfile的實例。一次只打開一個子窗體實例c#
我開始實施一個Singleton模式,其中途工作。如果NewProfile打開並且我轉到MainForm並嘗試創建NewProfile的另一個實例,它將起作用。如果NewProfile已被銷燬,則它不起作用,前進到下一個表單並且TwoProfile,ThreeProfile,FourProfile或FiveProfile中的一個已打開。它告訴我NewProfile.IsDisposed是真實的,給我一個對Singleton實例的錯誤引用。
我無法弄清楚如何做我的邏輯,這樣如果TwoProfile,ThreeProfile,FourProfile或FiveProfile中的一個打開,或者NewProfile本身打開,NewProfile將不會被創建。
我希望這是有道理的。我沒有太多的代碼可以發佈,除了我爲我的Singleton做的。
private static NewProfile _instance = null;
public static NewProfile Instance
{
get
{
if (_instance == null)
{
_instance = new NewProfile();
}
return _instance
}
}
謝謝:)
你爲什麼不直接使用含有用戶控件,比你一個窗體只需更改Mainform中的用戶控件,即可保證只打開「一個」表單並將所有用戶控件存儲在列表中,並且您可以下載或下載哪個UC按鈕,以便加載哪個UC加載 – Venson
您可以簡單地隱藏()NewProfile而不是在你打開SecondProfi時銷燬它樂? – Steve
你看過做MDI ..嗎? – MethodMan