Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What's the best way to broadcast a custom event from a .Net MDI parent window to it's MDI child forms?

Posted on 2011-02-26
2
Medium Priority
?
568 Views
Last Modified: 2012-05-11
Hi:

I'm working an .Net MDI based application written in C#.

I would like to create a custom event which when broadcasted from the MDI parent would be
received by all MDI child forms. (Much like a custom windows message in pre-.Net days).
The child window would then act on the message if interested.

I know I could brute force this by having a public interface exposed on my subclassed MDI child windows.
From the MDI Parent window, I could then iterate over all the children and call the newly defined/exposed interface.

I figured there must be a more "correct"/"graceful" way to do this.

Thanks,
JohnB
0
Comment
Question by:jxbma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 34989284
Sure...you declare an event in the MdiParent and then whenever an MdiChild is created you simply subscribe to that event.   When the MdiParent event is raised all subscribers will be notified automatically.

Here's my MdiParent (Form1):
public partial class Form1 : Form
    {

        public delegate void EventSignature(string message);
        public event EventSignature MdiParentEvent;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            this.MdiParentEvent += new EventSignature(f2.MdiChildHandler);
            f2.MdiParent = this;
            f2.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MdiParentEvent("Listen to your Parents! @" + DateTime.Now.ToString());
        }

    }

Open in new window


Here's my MdiChild (Form2):  
public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public void MdiChildHandler(string message)
        {
            this.Text = message;
        }

    }

Open in new window

0
 
LVL 1

Author Closing Comment

by:jxbma
ID: 34989308
Awesome.
Spot on.
Thanks fore getting back to me so quickly!
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

636 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