简体   繁体   中英

Communication between different forms

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?

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM