1

How do you get a value from one form in another form? I've tried the following but it doesn't work.

Form1:

public TextBox TXT()
{
get{return txtbox1;}
}

Form2:

public Form1 frm;

txtbox2.Text=frm.TXT.Text;
MusicLovingIndianGirl
  • 5,909
  • 9
  • 34
  • 65
user2837650
  • 23
  • 1
  • 1
  • 5
  • Do you have 2 separate forms? – MusicLovingIndianGirl Nov 23 '13 at 05:01
  • -1 for a poorly researched question. This is basic WinForms knowledge that is easily obtained, eg. in the MSDN Getting Started area: [How to: Retrieve Dialog Box Information Selectively Using Multiple Properties](http://msdn.microsoft.com/en-us/library/56taefba(v=vs.110).aspx). – groverboy Nov 23 '13 at 05:49

4 Answers4

14

Declare your Form1's txtbox1 Modifier as public . And in Form2 Declare the following:

System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["Form1"];

Then :

txtbox2.Text = ((Form1)f).txtbox1.Text;
yasmuru
  • 1,178
  • 2
  • 20
  • 42
1

Try the following.

Form1.cs

private Form2 secondForm;
private void GetSecondFormTextBox()
{
    textBox1.Text = secondForm.TextBox1.Text;
}

Form2.cs

public TextBox TextBox1
{
    get
    {
        return textBox1;
    }
}
MusicLovingIndianGirl
  • 5,909
  • 9
  • 34
  • 65
1

Form2

public String txtval { get; set; }

txtBox2.Text = txtval;

Form1

Form2 frm2 = new Form2();
frm2.txtval = txtBox1.Text;
senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36
0

Try to assign the Form1.TextBox1.Text into a Public Shared variable and access the variable into the Form2.TextBox1.Text

eirishainjel
  • 570
  • 7
  • 25