Access to GUI from another class
I've GUI with several Labels, Listboxes, Numeric开发者_如何学运维UpDown etc. I want read their values in another class. Standard properties of controls are private. How should I do this?
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.
public string SomeLabelValue
{
get { return label1.Text;}
}
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.
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
精彩评论