C# Winforms .Net - 两个窗体之间的动态通信
我有两个需要同时通信的人。当单击 FORM B 中的某些元素时,将显示 FORM A 并执行某些操作。但是当我在第一次操作后单击 FORM A 中的某个下一个按钮时,FORM B 中的 foreach 循环应该可以工作。
I've two froms which need to communicate simultaneously. When certain element in FORM B is clicked, FORM A is shown and some operation is performed. But the foreach loop inside FORM B should work when i click some next button in FORM A after the first operation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以修改
FormA
的构造函数将 FormB 中的表单显示为
现在您在 FormA 中引用了
FormB
>。现在在公共方法中公开循环逻辑,该方法可以由FormB
的此实例访问希望这会有所帮助。
Can you modify the constructor of your
FormA
Show the form from FormB as
Now you have a reference of
FormB
in FormA. Now expose the loop logic in a public method which could be accessed by this instance ofFormB
Hope this helps.
另一种选择是在 FormB 中创建您自己的事件并在 FormA 中订阅它们
以下是一些示例:
http://msdn.microsoft.com/en -us/library/8627sbea(v=vs.71).aspx
http://ondotnet.com/pub/a/dotnet/ 2002/04/15/events.html
http://www.switchonthecode.com/tutorials/csharp- snippet-tutorial-custom-event-handlers
这里还有一个关于如何执行此操作的很棒的示例:
如何在 C# 中创建自己的事件?
另请注意,虽然 V4Vendetta 提供的是执行此操作的简单方法,但这可能是执行此操作的更好方法,也是更好的实践。
Another option is to create your own events in FormB and subscribe to them in FormA
here is a few examples:
http://msdn.microsoft.com/en-us/library/8627sbea(v=vs.71).aspx
http://ondotnet.com/pub/a/dotnet/2002/04/15/events.html
http://www.switchonthecode.com/tutorials/csharp-snippet-tutorial-custom-event-handlers
Also here is a greate sample about how to do this:
How can I make my own event in C#?
Note also,while what V4Vendetta offered would be the easy way to do this-this however is probably the better way to do this,and also a better practice.