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.
LVL 1
BlearyEyeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
When you instantiate FormB, wire it up before you display it:
public partial class FormA : Form
    {
        public FormA()
        {
            InitializeComponent();
        }

        private void FormA_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(FormA_FormClosing);
        }

        void FormA_FormClosing(object sender, FormClosingEventArgs e)
        {
            Console.WriteLine("FormClosing() Event");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormB frmB = new FormB();
            frmB.FormClosing +=new FormClosingEventHandler(FormA_FormClosing);
            frmB.Show();
        }
    }

Open in new window

0
BlearyEyeAuthor Commented:
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();

Open in new window

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.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hmm...I'm not sure how to do that; or if that's even possible.  =\
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Sudhakar PulivarthiProject Lead - EngineeringCommented:
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.
0
BlearyEyeAuthor Commented:
Could you be a little clearer? How do I assign the same method?
0
Sudhakar PulivarthiProject Lead - EngineeringCommented:
Hi,

Please find the attached code.
We can make the closing event handler method as static, hence if your outside the source file/ class you can still access the method with FormA.FormA_FormClosing

public partial class FormA : Form
    {
        public FormA()
        {
            InitializeComponent();
        }

        private void FormA_Load(object sender, EventArgs e)
        {
            this.FormClosing += new FormClosingEventHandler(FormA_FormClosing);
        }

        public static void FormA_FormClosing(object sender, FormClosingEventArgs e)
        {
            Console.WriteLine("FormClosing() Event");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormB frmB = new FormB();
            frmB.FormClosing +=new FormClosingEventHandler(FormA_FormClosing);
            frmB.Show();
        }
    }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BlearyEyeAuthor Commented:
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.
0
BlearyEyeAuthor Commented:
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.
0
Sudhakar PulivarthiProject Lead - EngineeringCommented:
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
0
BlearyEyeAuthor Commented:
I have the source and what you've suggested will do for me, so I'm not actually stuck.

I am, however, puzzled that apparently the collection of handlers associated with an event is not first class: they can be assigned but they cannot be read and reassigned. The simplest solution for my question would be to read the form closing handlers for form A and assign them to form B: no need for access to the source code.
0
BlearyEyeAuthor Commented:
I included my final comment, since the solution was not what I was really looking for, but was good enough for my immediate need.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.