在任何类中访问子窗体对象 [英] Accessing Child Form object in any class

查看:92
本文介绍了在任何类中访问子窗体对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在C#.net 4.0中创建一个具有MDIParent和MDIChild表单的新MDI应用程序.
MDi Child具有其值存储在data.cs类中的网格. data.cs类的对象存储在MDIChild中.
现在,我们希望将data.cs中的所有值打印到报表中.
由于我是C#的新手,请给我建议如何通过report.cs文件中的MDIChild访问data.cs文件的对象.

预先表示感谢.

We are creating a new MDI application in C#.net 4.0 which has MDIParent and MDIChild Form.
MDi Child has grid whose values are stored in the data.cs class. Object of data.cs class is stored in MDIChild.
Now we want to print all values from data.cs to report.
As I am new to C#, give me suggestion how to access the object of data.cs file through MDIChild in report.cs file.

Thanks in advance.

推荐答案

尽管您可以循环浏览例如窗体上的Controls集合,但这感觉像是设计问题.

IMO不应在代码中直接使用不同形式的数据.而是尝试将逻辑和数据与表示(窗口及其控件)分开.这样可以更轻松地处理数据,并且是更健壮和可维护的解决方案.
While you can loop through for example Controls collection on a form, this feels like a design problem.

IMO you shouldn''t use the data directly from different forms in your code. Instead try separating the logic and the data from the presentation (the window and it''s controls). This would make it much easier to handle the data and would be more robust and maintainable solution.


如果Data类实例存储在MDI子类中,则您的报表类实例需要可以通过MDI子表单实例或其他方式访问特定的Data实例.

就个人而言,我宁愿将类尽可能地分开,而不是希望将表单数据提供给报表类,而要在MDI父级中获取数据,然后将其传递给Report类.构造函数.这样,报表类不需要知道MDI父级或子级(可能不是Data类)的存在-只需继续处理报表数据即可.然后可以在没有更改的情况下在其他项目中重用.
If the Data class instance is stored in the MDI child class, then your report class instance need to have access to the specific Data instance either via the MDI child form instance or some other way.

Personally, I would prefer to keep the classes as separate as possible, and instead of looking at making the form data available to the report class, I would get the data in the MDI parent, and pass it to the Report class, probably as part of the constructor. This way, the report class does not need to know about the existence of the MDI parent, or Child (and possibly not about the Data class) - it just gets on with the job of handling data for reports. It can then be reused in other projects without changes.


这篇关于在任何类中访问子窗体对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆