通过事件与用户控件交互
我浏览了网站上的问题,但尚未找到合适的答案。
我的问题是,由于所有事件和方法,我的主窗体类变得混乱。
我决定将表单分解为用户控件。不幸的是,我无法从其他用户控件访问信息。
例如,我有一个打印一些信息的用户控件。但其中一些信息位于另一个用户控件中。我将如何检索该数据?
我读到的一种解决方案是将委托传递给打印用户控件。该委托将包含信息用户控件中的一个方法,该方法从控件返回数据。因此,每当调用打印事件时,我都会调用该函数。
我的另一个解决方案是将一个事件附加到信息用户控件。每当用户控件中的信息发生变化时,我都会更新我的打印变量。
I've browsed through the questions on the site, but haven't found a decent answer yet.
My problem is that my main form class is cluttered due to all the events and methods.
I decided to break the form into user controls. Unfortunately I am having trouble accessing information from other User controls.
For example I have a user control that prints some information. But some of that information is in another User Control. How would I retrieve that data?
One solution I read was to pass a delegate to the Print User control. The delegate would contain a method from the Information User Control that returned the data from the control. So whenever the print event is called I would call that function.
The other solution I had was to attach an event to the information User Control. Whenever the information in the User Control changed, I would update my print variables.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么你需要为此使用事件?如果控件 X 需要来自控件 Y 的信息,则将控件 Y 传递给控件 X 的构造函数。
Why do you need to use events for this at all? If control X needs information from control Y, then pass control Y into control X's constructor.