开发者

Communication between different forms

In C#, using winforms, what is the best way to make forms talk to each other? Sending data, messages, st开发者_高级运维rings, 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜