How can I fire an event in a Windows Form and handle that event in a different form?

Working in C#: I have a form (A) which spawns a form (B), which in turn spawns a third form (C). When a button is pressed on Form C, I want certain text boxes on Form A to be cleared.
I have tried firing an event in Form C after setting up the "event wiring" as MS describes in its online docs. I get the runtime error that the event is null when I fire the event:

        protected void OnCalibrateEvent(CalibrateEventArgs e)
        {
            CalibrateEvent(e);
        }

I cannot figure out how to have Form A subscribe to the event.
jobremsAsked:
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:
Here is one way to do it...

I pass a reference to A into B via the Show() command.  This is accessible in B using the Owner() property.  Then we pass that reference to A into C where we can wire up the event:  
public partial class FormA : Form
    {
        public FormA()
        {
            InitializeComponent();
        }

        public void ClearHandler()
        {
            textBox1.Clear();
            textBox2.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FormB B = new FormB();
            B.Show(this); // <-- pass A to B
        }

    }

Open in new window

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

        private void button1_Click(object sender, EventArgs e)
        {
            FormC C = new FormC();
            C.Show(this.Owner); // <-- Pass A to C 
        }
    }

Open in new window

 
public partial class FormC : Form
    {

        public delegate void ClearTextBoxes();
        public event ClearTextBoxes Clear;

        public FormC()
        {
            InitializeComponent();
            this.Shown += new EventHandler(FormC_Shown);
        }

        void FormC_Shown(object sender, EventArgs e)
        {
            if (this.Owner != null && this.Owner is FormA)
            {
                this.Clear += ((FormA)this.Owner).ClearHandler;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Clear != null)
            {
                Clear();
            }
        }

    }

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
jobremsAuthor Commented:
Thanks for a carefully constructed solution. I didn't give all the info when I posted the question, unfortunately, and the solution won't work for me because the forms are all modal. And I can't pass a reference in the ctor of Form B because the program architecture uses a menu mechanism to call only a parameter-less ctor (and I don't want to mess with that mechanism!).

Luckily I have found a different approach to achieve what I want without using events, so all is well.
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
Microsoft Development

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.