views:

112

answers:

3

In C#, using winforms, what is the best way to make forms talk to each other? Sending data, messages, strings, whatever, from on to the other?

Delegates?

Ideas?

+3  A: 

You can create events in one form and then register for those events in the other form. You can also simply access properties from one form to the other. For example maybe in the constructor of the second form, you would pass a variable for the first form.

It sounds like what you're looking for are events though. When some event happens any delegate that is registered will be called.

There is a tutorial on MSDN for events here.

Brian R. Bondy
+2  A: 

We'ved used something called the Event Pattern successfully in several Winform applications. Here's a good link that will help you get started.

Walter
+1  A: 

all depends on what you want to communicate.

Let's say it is configuration data; You could create a static property on main form called Settings, which would expose your object. Than all forms would see that same Settings instance, and all would see any changes.

for extra credit you could implement INotifyPropertyChanged, and have it trigger an event. that way all forms looking at Settings would be notified if anything changed.

Sonic Soul