How to get variable from form1 to form2 with { get; set;}?

In your Form2 you are using a public property, because it is public you can assign it via the object in form1. For example:

 private void button1_Click(object sender, EventArgs e)
        {
            Form2 dlg = new Form2();
            dlg.test = "test this point";
            dlg.Show();
        }

There are a couple of ways to use this in form 2, if you just want it to set the text property of the label only, this would be the best:

public partial class Form2 : Form
    {
        public string test 
        { 
           get { return label1.Text; }
           set { label1.Text = value ;} 
        }

        public Form2()
        {
            InitializeComponent();
        }
    }

Within the setter of the property you could also call a function if required.

Leave a Comment