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
557 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
Comment Utility
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
Comment Utility
Awesome.
Spot on.
Thanks fore getting back to me so quickly!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now