How to: Get a Value from Another Form (Visual C#) 

This example retrieves a value from a text box on a Windows Form and displays it in a text box on another form.

Example

In Form1.cs:

private Form2 otherForm;
private void GetOtherFormTextBox()
{
    textBox1.Text = otherForm.TextBox1.Text;
}

In Form2.cs:

public TextBox TextBox1
{
    get
    {
        return textBox1;
    }
}

Compiling the Code

This example requires:

  • Two forms named Form1 and Form2, each containing a TextBox control named textBox1. Form1 should create an instance of Form2 and assign it to otherForm; GetOtherFormTextBox will copy the text in Form2's textBox1 to Form1's textBox1.

See Also

Other Resources

Visual C# Express
C# Programming Examples