search
Twitter Rss Feeds
MicrosoftArticlesForumsGroups
C# .NET
VB.NET
Visual Studio .NET
ADO.NET
Xml/Xslt
VB 6.0
.NET CF
GDI+
LINQ
Deployment
Security
FoxPro
Silverlight / WPF
Entity Framework
RIA Services

Web ProgrammingArticlesForumsGroups
JavaScript
ASP
ASP.NET
Web Services

Non-MicrosoftArticlesForumsGroups
NHibernate
Perl
PHP
Ruby
Java
Linux / Unix
Apple
Open Source

DatabasesArticlesForumsGroups
SQL Server
Access
Oracle
MySQL
Other Databases

OfficeArticlesForumsGroups
Microsoft Excel
Microsoft Word
Microsoft Powerpoint
Publisher
Money

Operating SystemsArticlesForumsGroups
Windows 7
Windows Server
Windows Vista
Windows XP
Windows Update
MAC
Linux / UNIX

Server PlatformsArticlesForumsGroups
Share Point
BizTalk
Site Server
Exhange Server
IIS
Transaction Server

Graphic DesignArticlesForumsGroups
Macromedia Flash
Adobe PhotoShop
Microsoft Expression

OtherArticlesForumsGroups
Subversion / CVS
Ask Dr. Dotnetsky
Active Directory
Networking
Uninstall Virus
Job Openings
Reviews
Search Engines
Resumes

 
MDI Winforms Inter-form Communication With Events
By Peter A. Bromberg, Ph.D.
Printer - Friendly Version
Peter Bromberg

There are many ways to communicate from one form to another in a Windows Forms .NET application. However, when using MDI (Multiple Document Interface) mode, you may often find that the wiring up of simple events may better suit your needs. This short article illustrates one of the simplest techniques for having a Child MDI form call back to its parent Container Form and even how to pass an object instance (which could contain an entire class, if you like) back to the Parent.

Fire up Visual Studio .NET and create a new Windows Application project. In the Properties window with your Default Form1 selected, change the IsMDIContainer property to "true". Add a Panel at the top, and add a button and a label to it, with the Panel taking up, say, the top 20% of the form's designer surface.

Now add a new form to the project. Let's call it MDIChild. On the Kid form, add a button which we will use to call the event to the Daddy form and also close the Kid.

 

At this point, your Daddy form should look something like this:

Now lets wire up our event and event handlers, starting with the Kid:

First we need a custom EventArgs derived class so we can pass the information we need:

using System;
using
System.Collections.Generic;
using
System.Text;

namespace MDIFormsEvents
{
public class NotifyDaddyEventArgs :System.EventArgs
{
public readonly string Message;
public NotifyDaddyEventArgs(string message)
{
Message = message;
}
}
}

 

In our Codebehind for the Kid form, lets add our event and eventhandler, and make the call in our Button1_Click event:

// declare the EventHandler
public event EventHandler NotifyDaddy;
// Wire up the event
protected void OnNotifyDaddy()
{
if(NotifyDaddy !=null)NotifyDaddy(this,new NotifyDaddyEventArgs("Howdy Pop!"));
}

private void button1_Click(object sender, System.EventArgs e)
{
// call the event
this.OnNotifyDaddy();
this.Close();

}

Note that we are passing "Howdy Pop" in the EventArgs parameter. This can really be any business logic you want. If your Kid form created a DataSet that you needed for the Daddy to receive, you would plug it in here. All Daddy would need to know is that he is expecting a DataSet from the Kid in the EventArts parameter.

Now, in our Daddy Form, we are going to wire up everything we need to show the kid and also to receive his event message:

private void button1_Click(object sender, System.EventArgs e)
{
// declare and instantiate the Kid
MDIChild chForm = new MDIChild();
//set parent form for the child window
chForm.MdiParent=this;
// make it fill the container
chForm.WindowState=FormWindowState.Maximized;
// add the event handler we wired up
chForm.NotifyDaddy+=new EventHandler(chForm_NotifyDaddy);
// show the Kid
chForm.Show();
}

private void chForm_NotifyDaddy(object sender, EventArgs e)
{
lblMessage.Text=
" Child Form Called me! and sent:\n";
lblMessage.Text+= ( (
NotifyDaddyEventArgs)e).Message+"\n" ;
lblMessage.Text+=e.GetHashCode().ToString();
}

Note that we create chForm.NotifyDaddy and add chForm_NotifyDaddy to the Delegates. Then in the handler itself, we write out the message including the value of the sender (in this case it is just some text). Your result when the Kid form is closed should look something like this:

And that's all it takes to teach all your kids to call home to Daddy! The full solution is ready to go at the link below.

Download the Source Code that accompanies this article

 

 

Peter Bromberg is a C# MVP, MCP, and .NET consultant who has worked in the banking and financial industry for 20 years. He has architected and developed web - based corporate distributed application solutions since 1995, and focuses exclusively on the .NET Platform. Pete's samples at GotDotNet.com have been downloaded over 41,000 times. You can read Peter's UnBlog Here.  --><--NOTE: Post QUESTIONS on FORUMS!
Do you have a question or comment about this article? Have a programming problem you need to solve? Post it at eggheadcafe.com forums and receive immediate email notification of responses.


Pete's Blog   |    Pete's Resume   |    Robbe's Blog   |    Robbe's Resume   |    Archive #2   |    Archive #3   |    Dotnetslackers   |    XmlPitStop   |    Advertise   |   Contact Us   |   Privacy   |   Copyright (c) 2000 - 2009 eggheadcafe.com  All rights reserved.