Geeks With Blogs

News
Jason Coyne Jason Coyne's blog

Communicating elegantly from a child to a parent in programming is something that it took me a long time to figure out.  I tried all sorts of kludgy solutions, like passing in a reference to the parent as a parameter to the constructor/method of the child, etc.  This invariably leads down a bad path.  Of course, the correct solution is events.  The event model is something every developer uses all the time, because of the seamless way Visual Studio (or any other modern IDE) automatically generate event handlers for you when you do things like double click on buttons in the designer. However, creating custom events, and passing data via the event is something that eludes many.

I recently saw a post on egghead cafe where a user was asking this question, and the reply that he got was leading him down the same dead-end I described above.  I posted out an event example on egghead, but thought I would post it here as well to get a bit more visability to the solution.  The code below is nothing unique, its been written a million times before, but it took me a while to figure it out the first time, so hopefully someone gets some use out of it.

 

Here is an example of some an event I raise in one of my ASP.net forms. Very simmilar code will work with no problems in winforms.
Put this first snippet anywhere inside your namespace (but not inside any other class)

 
//Create a new event handler delegate, so that you can pass information back to the form. You could use the standard EventHandler, but then you can't pass any info back.
public delegate void SearchUpdateEventHandler(object sender, SearchUpdateEventArgs e);

//Here is a class that you use to hold the info that gets passed back. It has to decend from EventArgs.
public class SearchUpdateEventArgs : EventArgs
{
public SearchQuery searchQuery;

public SearchUpdateEventArgs(SearchQuery searchQuery)
{
this.searchQuery = searchQuery;
}
}


Then, in your control, have the following code

 
//Create the event so your form can listen for it
public event SearchUpdateEventHandler UpdateSearchCriteria;

//In your button/link, raise the event back to the form.
private void lnk_Click(object sender, EventArgs e)
{
searchQuery.SearchText += " " + ((LinkButton) sender).Text;
Results = searchQuery.Search();

//This line makes it so you only raise the event if someone is listening.
if (UpdateSearchCriteria!=null)
//Raise the event, and pass in the data (searchQuery in this case) that you want to send to the form.

UpdateSearchCriteria(this, new SearchUpdateEventArgs(searchQuery));
}



Finally, in your main form, you have code that listens for the event. This is just like any other event you have on your form for buttons etc.

These last two snippets can be generated automatically using the events (lightning bolt) button on the properties window at design time.

Put this line of code somewhere in your onLoad (or the winforms equivilent)

 
SearchResultDisplay3.UpdateSearchCriteria+=new SearchUpdateEventHandler(SearchResultDisplay3_UpdateSearchCriteria);



and then your actual event (just like normal buttons)

 
private void SearchResultDisplay3_UpdateSearchCriteria(object sender, SearchUpdateEventArgs e)
{
//Or set your textbox or whatever
SearchCriteria1.UpdateForm(e.searchQuery);
}
Posted on Monday, February 20, 2006 12:34 PM Programming , c# | Back to top


Comments on this post: Elegant communication from child to parent in .net via events

# re: Elegant communication from child to parent in .net via events
Requesting Gravatar...
I found youer article useful. however i have a question:

public class SearchUpdateEventArgs : EventArgs{
public SearchQuery searchQuery;

In the above class "SearchQuery" is a member of the child object itself or is it any other object used to simply pass data across from child to parent? In the later cases, do I need to delcare the class in both child and parent if both reside in different modules? (eg I have a component as child and need to pass something to the parent (a client WinForm app that will contain my component)).

Please reply at my email:samladi@bnybrokerage.com

Left by Sanjay Amladi on Oct 12, 2006 8:51 PM

# re: Elegant communication from child to parent in .net via events
Requesting Gravatar...
Thank you
Left by Jesper on Dec 07, 2006 6:04 AM

# re: Elegant communication from child to parent in .net via events
Requesting Gravatar...
Thanks for the great article. The only suggestion I have (if I HAD to have one... :) was to explicitly state what goes in parent and what goes in child. I ended up figuring it out anyway though.

Thanks again,
Todd
Left by Todd on Jan 21, 2010 2:36 AM

Your comment:
 (will show your gravatar)


Copyright © Jason Coyne | Powered by: GeeksWithBlogs.net