Passing a value from one form to another form

I have two forms named form1 and form2:

  • form1 is made of a label and a button.
  • form2 is made of a textBox and a button

When I click the form1 button, this will show up form2. Any inputs in textBox should be written back to form1.label once I hit the button in form2.
I have the code below but it doesn't work.

// Code from Form 1
public partial class Form1 : Form
{
    public void PassValue(string strValue)
    {
        label1.Text = strValue;
    }
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 objForm2 = new Form2();
        objForm2.Show();
    }

}

// Code From Form 2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 objForm1 = new Form1();
        objForm1.PassValue(textBox1.Text);
        this.Close();
    }
}

And a screenshot:

Application screenshot

How can I realize that?


You don't access your form1, from which you created form2. In form2 button1_Click you create new instance of Form1, which is not the same as initial. You may pass your form1 instance to form2 constructor like that:

   // Code from Form 1
 public partial class Form1 : Form
{
    public void PassValue(string strValue)
    {
        label1.Text = strValue;
    }
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 objForm2 = new Form2(this);
        objForm2.Show();
    }

}

// Code From Form 2

public partial class Form2 : Form
{
    Form1 ownerForm = null;

    public Form2(Form1 ownerForm)
    {
        InitializeComponent();
        this.ownerForm = ownerForm;
    }

    private void button1_Click(object sender, EventArgs e)
    {       
        this.ownerForm.PassValue(textBox1.Text);
        this.Close();
    }
}

Like mentioned in other posts, you won't be able to reference the original Form1 by creating a new instance of Form1. You can pass Form1 into Form2's constructor or expose Form2's text as a public property, but I usually prefer using delegates for this to maintain loose coupling.

// Code from Form 1
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 objForm2 = new Form2();
        objForm2.PassValue += new PassValueHandler(objForm2_PassValue);
        objForm2.Show();
    }
    public void objForm2_PassValue(string strValue)
    {
        label1.Text = strValue;
    }
}

// Code From Form 2
public delegate void PassValueHandler(string strValue);
public partial class Form2 : Form
{
    public event PassValueHandler PassValue;

    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (PassValue != null)
        {
            PassValue(textBox1.Text);
        }
        this.Close();
    }
}

When you are doing:

Form1 objForm1 = new Form1();
objForm1.PassValue(textBox1.Text);

... you are creating a new Form1 and calling the PassValue method on the wrong Form1 object. Instead, you could do:

public partial class Form1 : Form
{
    // This is the text that will be entered in form2
    public String form2text;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Show form2
        Form2 objForm2 = new Form2(this);
        objForm2.ShowDialog();
        // When form2 is closed, update the label text on form1
        label1.Text = form2text;
    }
}

public partial class Form2 : Form
{
    // This is the instance of Form1 that called form2
    private Form1 form1caller;

    public Form2(Form1 form1caller)
    {
        InitializeComponent();
        this.form1caller = form1caller;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Pass the textBox value to form1 before closing form2
        form1caller.form2text = textBox1.Text;
        this.Close();
    }
}

I just tried this code and it works, sure it will help you.

in the first form (Form1) type below:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f = new Form2(textBox1.Text);
        f.ShowDialog();
    }
}

in the second form (Form2) use below codes:

public partial class Form2 : Form
{
    public Form2( string st)
    {
        InitializeComponent();
        textBox1.Text = st;
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }
}