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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

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…
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…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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