【发布时间】:2013-01-19 19:23:17
【问题描述】:
我正在编写一个应用程序,该应用程序使用一系列类似向导的 5 个简单表单。第一个表单 NewProfile 是从主应用程序 MainForm 上的菜单项打开的,MainForm 的子表单也是如此。第二种形式,TwoProfile,是从 NewProfile 上的一个按钮打开的。第三种形式,ThreeProfile 是从 TwoProfile 上的一个按钮打开的,对于所有 5 个形式,依此类推。这是顺序: MainForm --> NewProfile TwoProfile ThreeProfile FourProfile FiveProfile。我的问题是,当打开任何表单(NewProfile、TwoProfile、ThreeProfile、FourProfile 或 FiveProfile)时,我不希望用户能够创建 NewProfile 的实例。
我从实现一个单例模式开始,它在中途工作。如果 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
}
}
谢谢你:)
【问题讨论】:
-
你为什么不只使用一个包含用户控件的表单,而只是更改主窗体中的用户控件,这将确保你只打开“一个”表单并且所有用户控件都存储在列表中以及您可以处理加载哪个 UC 的 Next 或 Back 按钮
-
您能否在打开 SecondProfile 时简单地隐藏() NewProfile 而不是销毁它?
-
你有没有看过做 MDI..?