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
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
559 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
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

860 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