?
Solved

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

Posted on 2011-10-21
2
Medium Priority
?
176 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:jobrems
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 37009840
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
 

Author Closing Comment

by:jobrems
ID: 37018807
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question