Skip to content

Get value from other form

tomekalaj

Asked Oct 20, 2005, 9:17 AM EDT2 answers
Share
LinkedInX

I have one open Form1. I show Form2 (but Form1 is stile open)  and when I close Form2 I nee to take value from  Form2.textBox1.text in Form1.textBox1.text . 

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    When you intialize the form2 in form1 send the reference of form1 to constructor of form2 as following

    Form2 frm2 = new Form2(this);
    frm2.Show();

    create an object of form1 in form2 and the constructor for form2 should be as following

    private Form1 frm1;
    public Form2(Form1 refForm)
    {
        InitializeComponent();
        this.frm1 = refForm;
    }

    and then in the Closing event of form2 write the following code

    C#
    private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
         this.frm1.textBox1.Text = this.textBox1.Text;
    }
    0
  • 1 short reply
    • 0
      Thank for help
      0