storing data in another form protection issues

谁都会走 提交于 2019-12-24 15:43:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!