BlearyEye
asked on
Copy event handler in C#
I'm using C# 4.0 (VS 2010). In the code for form A, I need to create form B and give B the same OnClosing event handler as A. Is there some way to do this? I don't seem to be able to do a simple copy.
ASKER
What you're suggesting is assigning the same event handler to B as to A. In my case, I don't have direct access to A's event handler. It's like this:
Pgm:
FormA frmA = new FormA();
frmA.FormClosing += new FormClosingEventHandler(someHandlerInPgm);
frmA.Show();
The method someHandlerInPgm is in Pgm and is not in scope for B. So when I instantiate B, I need to copy whatever handler A has over to B.
Hmm...I'm not sure how to do that; or if that's even possible. =\
Hi,
Onclosing event handler code if its not depended on instance variable the write a static method which is accessible outside the class and assign the same method for handling the event.
Onclosing event handler code if its not depended on instance variable the write a static method which is accessible outside the class and assign the same method for handling the event.
ASKER
Could you be a little clearer? How do I assign the same method?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hmm ... I'll give it a try as soon as I can. But I have a doubt. The form closing handler is specific to a particular A object, not to the class itself. So I don't see how it would work.
ASKER
I see now why it works. It does depend on having access to the FormClosing method that that FormA uses, however. It would be nice to be able to make the copy without knowing having access to the source.
Hi BlearyEye,
Why do you need a copy?
Has your code need to do with inheritance between FormA and FormB.
Whether any other third party will write code extended for FormB.
Please clearly specify the reason for Copy of two sources. Helps to identify and specify a good code.
Regards,
Sudhakar
Why do you need a copy?
Has your code need to do with inheritance between FormA and FormB.
Whether any other third party will write code extended for FormB.
Please clearly specify the reason for Copy of two sources. Helps to identify and specify a good code.
Regards,
Sudhakar
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I included my final comment, since the solution was not what I was really looking for, but was good enough for my immediate need.
Open in new window