title: Passing data between two forms with properties
body: <p>I am passing data between 2 windows forms in C#. Form1 is the main form, whose textbox will receive the text passed to it from form2_textbox &amp; display it in its textbox (form1_textbox).</p>
  
  <p>First, form1 opens, with an empty textbox and a button, on clicking on the form1_button, form2 opens. In Form2, I entered a text in form2_textbox &amp; then clicked the button (form2_button).ON click event of this button, it will send the text to form1's textbox &amp; form1 will come in focus with its empty form1_textbox with a text received from form2.</p>
  
  <p>I am using properties to implement this task. FORM2.CS</p>
  
  <p>public partial class Form2 : Form
  {
      //declare event in form 2
      public event EventHandler SomeTextInSomeFormChanged;</p>
  
  <pre><code>public Form2()
  {
      InitializeComponent();
  
  }
  public string get_text_for_Form1
  {
      get { return form2_textBox1.Text; }
  }
  
  //On the button click event of form2, the text from form2 will be send to form1:
  
  public void button1_Click(object sender, EventArgs e)
  {
      Form1 f1 = new Form1();
      f1.set_text_in_Form1 = get_text_for_Form1;
  
  //if subscribers exists
  if(SomeTextInSomeFormChanged != null)
  {
      SomeTextInSomeFormChanged(this, null);
  }
  
  }
  
  }
  </code></pre>
  
  <p>FORM1.CS</p>
  
  <pre><code>public partial class Form1 : Form
  {
      public Form1()
      {
          InitializeComponent();
      }
  
      public string set_text_in_Form1
      {
          set { form1_textBox1.Text = value; }
      }
  
      private void form1_button1_Click(object sender, EventArgs e)
      {
          Form2 f2 = new Form2();
          f2.Show();
          f2.SomeTextInSomeFormChanged +=new EventHandler(f2_SomeTextInSomeFormChanged);  
      }
  
      //in form 1 subcribe to event
      Form2 form2 = new Form2();
  
      public void f2_SomeTextInSomeFormChanged(object sender, EventArgs e)
      {
          this.Focus();
  
      }
  }
  </code></pre>