Search code examples
c#formsmultiple-forms

Transferring information between forms


I've been working on this project for about an hour now and I got stuck. I've got 4 forms but only the last 3 is relevant. In form 2 I use:

    this.Visible = false;
    Form3 Form3 = new Form3();
    Form3.Show();

To create and show form 3. Form3 also got a textbox which is empty and I want to transfer that info to a label in Form4. In form 3 I use the same cod as in Form 2 to make Form 3.

I've tried a couple of things and searched on the forums but nothing seems to work...

     lblN2.Text = Form3.txtf.Text;

I want to transfer the text that the user writes in the textbox(txtf) in Form3 to a empty label(lblN2) in Form4.


Solution

  • Try something like this (code in Form3 class):

        Form4 frm4 = new Form4();
        frm4.lblN2.Text = this.txtf.Text;
    frm4.Show();
    

    Alternative would be to modify constructor method in Form4 to accept string parameter and invoke it as follows:

     Form4 frm4 = new Form4(this.txtf.Text);
        frm4.Show();