title: passing data between two forms using 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.
  <strong>FORM2.CS</strong></p>
  
  <pre><code>public partial class Form2 : Form
  {
      //declare event in form 2
      public event EventHandler SomeTextInSomeFormChanged;
  
      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>
  
  <hr>
  
  <p><strong>FORM1.CS</strong></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>
  
  <p>Now, in this case I have to again SHOW the form1 in order to automatically get the text in its textbox from form2, but I want that as I click the button on form2, the text is sent from Form2 to Form1, &amp; the form1 comes in focus, with its textbox containing the text received from Form2.</p>