从另一个类访问 GUI
我有带有几个标签、列表框、NumericUpDown 等的 GUI。我想在另一个类中读取它们的值。控件的标准属性是私有的。我该怎么做?
I've GUI with several Labels, Listboxes, NumericUpDown etc. I want read their values in another class. Standard properties of controls are private. How should I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信最合适的方法是将这些 GUI 元素封装到属性中并通过 getter 公开它们的数据。例如,
这可以保护您的元素仅被读取,同时从对象中暴露最少的数据。
但是,您可以公开整个元素和/或允许 setter 方法更改元素的值(如果适用)。
I believe the most appropriate way to do it would be to encapsulate these GUI elements into properties and expose their data via a getter. e.g.
This protects your element to only being read, while exposing the least amount of data from your object.
You can however expose the entire element and/or allow a setter method to change the values of the element if applicable.
向表单类添加一个方法,该方法返回您希望提供的所有值。例如,将您希望返回到另一个类的所有值放入一个结构中,然后返回该结构。
Add a method to your form class that returns all the values that you wish to make available. For example, put all the values that you wish to return to the other class into a struct, and return that struct.
您需要创建公共 getter(如果要更改值,还需要创建 setter)。
这是许多教程之一:
http://www.java2s.com/Tutorial/ CSharp/0140__Class/PropertyGetterandSetter.htm
You need to create public getters (and setters, if you want to change the values).
Here is one of many tutorials:
http://www.java2s.com/Tutorial/CSharp/0140__Class/PropertyGetterandSetter.htm