This is somewhat different question or maybe a easy question. but I have this problem now.
I have three forms loginForm, mainForm and subForm.
In my loginForm I have two accounts, one for mainForm access and other for subForm access.
The mainFormAccessAccount can access both mainForm and subForm but the subFormAccessAccount can only access subForm.
Through mainForm we can create multiple instances of subForm (mainForm is single instance).
Now my problem is: I want to implement different subForm_Closed Event functions for a subForm and its Instances (instances created by mainForm).
I used the below code to create the instances of subForm in subForm.cs
private mainForm MainForm;
internal void RegisterParent(mainForm form)
{
this.MainForm = form;
}
and in mainForm.cs to create instance of subForm, I used the below code:
subForm newSubForm = new subForm();
newSubForm.Show();
newSubForm.RegisterParent(this);
How can I solve this issue?
(I am not sure whether they are called instances or not because I am a Dot net noob)
Thanks in Advance.
If I understand your problem correctly you want two different handlers for close event of the SubForm.
As you suspected, this is indeed an easy problem and since you mentioned you are a .net noob I will try to explain in detail.
If I am not wrong you generated the event handler
subForm_Closedusing Visual studio designer surface. This seems to be the cause of your confusion.What does the Visual Studio Designer do to generate event handler:
If you open SubForm.cs notice the definition of its constructor. It will be something like this
This
InitializeComponentmethod is described in SubForm.designer.cs file () (expand SubForm.cs in solution explorer and you will be able to see it).One of the lines in
InitializeComponentmethod will be something like thisSo in effect as soon as you create a SubForm ‘instance’ (here I mean real object instance and not in the sense you mentioned in your question which is at best a child form) either through subFormAccessAccount or through MainForm, it attached your SubForm_Closed event handler to the FormClosed event.
How can you get the desired behavior?
If you want to handle the closed event in SubForm.cs, you can do something like this
If you want to handle the closed event in MainForm.cs, you can do like this