问题
I'm trying to store a string from one form to a label on another from. However, when doing so it says it cannot be done because of its protection level. Any ideas on how to fix this?
maskedTxtLogin.Text = FormInvisible.lblInitials.Text();
回答1:
The controls are generated as a private field in your form designer:
private System.Windows.Forms.Label lblInitials;
If you want to access them outside of the form you need to create a property for them. To see the above declaration and write a property for that, just right click on your lblInitials
in your code and click Go to Declaration (or Definition), in the class that you are navigated to write the following code:
public Label LblInitials
{
get { return lblInitials; }
set { lblInitials= value; }
}
Also you need to create a new instance of your form to access this property:
FormInvisible fr = new FormInvisible();
maskedTxtLogin.Text = fr.LblInitials.Text;
回答2:
This is another way to change the protection level
Open FormInvisible.Designer.cs
Look for private System.Windows.Forms.Label lblInitials;
Modify the access modifier from private
to public
or you can open the property of the control and change Modifiers
from private
to public
来源:https://stackoverflow.com/questions/53613774/storing-data-in-another-form-protection-issues