这是一个有点不同的问题或者可能是一个简单的问题。但我现在有这个问题。
我有三种表单loginForm
,mainForm
和subForm
。
在我的loginForm
我有两个帐户,一个用于mainForm
访问,另一个用于subForm
访问。
mainFormAccessAccount
可以访问mainForm
和subForm
,但subFormAccessAccount
只能访问subForm
。
通过mainForm
,我们可以创建subForm
的多个实例(mainForm
是单个实例)。
现在我的问题是 :我想为subForm_Closed
及其实例实现不同的subForm
事件函数(由{{创建的实例1}})。
我使用以下代码在 subForm.cs
中创建mainForm
的实例
subForm
并在 mainForm.cs 中创建private mainForm MainForm;
internal void RegisterParent(mainForm form)
{
this.MainForm = form;
}
的实例,我使用了以下代码:
subForm
我该如何解决这个问题?
(我不确定他们是否被称为实例,因为我是Dot net noob)
先谢谢。
答案 0 :(得分:2)
如果我正确理解你的问题,你需要两个不同的处理程序来关闭SubForm的事件。
如你所料,这确实是一个容易出问题的问题,因为你提到你是一个.net noob,我会试着详细解释。
如果我没有错,你使用Visual studio designer surface生成了事件处理程序subForm_Closed
。这似乎是你混淆的原因。
Visual Studio Designer如何生成事件处理程序:
如果打开SubForm.cs,请注意其构造函数的定义。它会是这样的
public SubForm()
{
InitializeComponent();
//May be some other code as well
}
在SubForm.designer.cs文件()中描述了这个InitializeComponent
方法(在解决方案资源管理器中展开SubForm.cs,你将能够看到它)。
InitializeComponent
方法中的一行将是这样的
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.SubForm_Closed);
因此,一旦你创建了一个SubForm'实例'(这里我的意思是真实的对象实例而不是你在你的问题中提到的那种最多只是一个子形式的意义上),通过subFormAccessAccount或通过MainForm,它附加了您的SubForm_Closed事件处理程序到FormClosed事件。
如何获得理想的行为?
如果你想在SubForm.cs中处理已关闭的事件,你可以这样做
internal void RegisterParent(mainForm form)
{
this.MainForm = form;
this.FormClosed -= SubForm_Closed; //Unhook previous handler
this.FormClosed += SubFormAsChild_Closed; //hook new handler
}
如果你想在MainForm.cs中处理已关闭的事件,你可以这样做
internal void RegisterParent(mainForm form)
{
this.MainForm = form;
this.FormClosed -= SubForm_Closed;
}
//in MainForm.cs
newSubForm.RegisterParent(this);
newSubForm.FormClosed += newSubForm_Closed;
答案 1 :(得分:1)
要走几条路 将另一个构造函数添加到SubForm
e.g。
public SubForm(Boolean argCreatedByMainForm) : this()
{
// save argument in private member variable for use in OnCloseQuery
}
然后使用
subForm newSubForm = new subForm(true);
newSubForm.Show();
newSubForm.RegisterParent(this);
这不是我的方式,因为让一个表格了解另一个表格,往往会变成维护噩梦,但是你需要在编程盒中使用更多工具来实现更好的解决方案。例如,一个用于管理表单之间交互的类,然后将它们抽象出接口,最后但并非最不重要的是注入行为。
看到你正在学习,让构造函数工作,你可以在你拿起技术时愚蠢地变得聪明
答案 2 :(得分:0)
subForm newSubForm = new subForm();
newSubForm.Show();
newSubForm.RegisterParent(this);
newSubForm.Close += (s, e) =>
{
// Close event will be fired for this instance only.
};
答案 3 :(得分:0)
我建议您使用mainform的其他结构 - 子表单引用。
您的Subform是从System.Windows.Forms.Form中获取的,它是从System.Windows.Forms.Control派生的,它实际上已经有一个属性来存储父控件: 所以你应该在那里存储对主表单的引用。
另一件事是你应该实现一个静态方法来在你的mainform中创建子表单,它实际上调用单实例引用的私有方法来创建真正的子表单 - 我假设你是单例模式。
因此代码看起来像这样
public class MainForm : System.Windows.Forms.Form
{
// used to hold references to subforms note: not static
private List<SubForm> mySubForms;
// singelton implementation
private static MainForm theInstance = null;
private MainForm()
{
mySubForms = new List<SubForm>();
}
public static MainForm GET_INSTANCE()
{
if (MainForm.theInstance == null)
{
MainForm.theInstance = new MainForm();
}
return MainForm.theInstance;
}
// creates subforms and the references right not:static method as singelton
//pattern is used
public static SubForm CREATE_SUBFORM()
{
SubForm newSub = new SubForm();
newSub.Parent = theInstance;
theInstance.mySubForms.Add(newSub);
return newSub;
}
}
如果代码有一些输入错误,请aplogize ...我现在没有IDE。我没有意识到我没有实现singelton模式线程安全......这对于演示目的来说是个不错的选择。
编辑: 更好的做法是让表格通过使用evetns进行交流。如果您计划拥有多线程应用程序,这将是最好的方法。但是你需要先进的.Net技能 - imo。