更新时间:2022-11-23 13:07:07
您属性是一个实例变量,所以值可以是跨 Form1中。
Your property is an instance variable, so the value can be different across different instances of Form1
.
如果你正试图从一个父窗体访问实例变量,要做到这一点最简单的方法是通过Form1中在到窗体2的构造
If you are trying to access instance variables from a parent form, the easiest way to do that is to pass Form1 in to the constructor of Form2.
public partial class Form2 : Form
{
private Form1 f1;
public Form2(Form1 ParentForm)
{
InitializeComponent();
f1 = ParentForm;
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = f1.Test;
}
}
然后,当你创建Form1中一个新的窗体2,你能做到这一点:
Then when you create a new Form2 from Form1, you can do this:
Form2 frm2 = new Form2(this);
如果你想成为只读你的财产,你根本无法指定setter方法:
If you want your property to be read only, you can simply not specify a setter:
public string Test
{
get { return _test; }
}