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
562 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 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