C# winform: Accessing public properties from other forms & difference between static and public properties

Your property is an instance variable, so the value can be different across different instances of Form1.

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;
    }
}

Then when you create a new Form2 from Form1, you can do this:

Form2 frm2 = new Form2(this);

If you want your property to be read only, you can simply not specify a setter:

public string Test
{
    get { return _test; }
}

Leave a Comment