-1

I have two forms form1 and form2. I want to get the text from the textbox of form2 when a button is clicked on form1. I am using on form1:

private void but_Click(object sender, EventArgs e)
{
    Form2 f2=new Form2();
    txtonform1=f2.fo;
}

and on form2 I have this method to return the text from the textbox:

public string fo
{            
    get { return textBox1.Text; }
    set { textBox1.Text = value; }
}

Now the problem is that it returns null. Whats the problem I am new to c# can anybody help me please!

user3115728
  • 33
  • 1
  • 8

4 Answers4

1

You have to work with one single form, otherwise you create new instance every time:

Form2 f2 = new Form2();

private void but1_Click(object sender, EventArgs e)
{
    f2.fo=txtonform1.Text;
}

private void but2_Click(object sender, EventArgs e)
{
    MessageBox.Show(f2.fo);
}
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
0

You are creating a new form instance here:

 Form2 f2=new Form2();

And your fo property return this new form's textBox1, so you textBox1 doesn't contain any text and you are getting null.

I guess you are displaying form2 from Form1, if it's correct just define a one Form2 intance in class level:

public partial class Form1 : Form
{
   Form2 f2 = new Form2();
}

And when you want to show it use this:

f2.Show(this);

When you want to change your TextBox value now you can use:

txtonform1.Text = f2.fo;

But to do this make sure you change your textBox1.Text in form2.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
0

You should keep the reference of form2 which is/was already displayed, in form1 and then use the same variable to access the value.

I don't know how form2 was created and shown but assuming it's created and shown by some button click on form1 then form1 class will look something like,

private Form f2 = null;   

private void buttonShowForm2_Click(object sender, EventArgs e)
{
    if(f2 == null)
        f2 = new form2();
    f2.Show();
}

private void but_Click(object sender, EventArgs e)
{
    if(f2 == null)  //If this form was not already displayed display it to get the input from user
       buttonShowForm2_Click(null, null);
    else
       txtonform1=f2.fo;
}
Deepak Bhatia
  • 6,230
  • 2
  • 24
  • 58
0

FIRST SOLUTION:

1.) Goto Form2 then Double click it. At the code type this.

public Form2(string sTEXT)
{
InitializeComponent();
textBox1.Text = sTEXT;
}

2.) Goto Form1 then Double click it. At the code type this.
//At your command button in Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 sForm = new Form2(textBox1.Text);
sForm.Show();
}

SECOND SOLUTION:

1.) Goto Form1 then Double Click it. At the code type this.

public string CaptionText
{
get {return textBox1.Text;}
set { textBox1.Text = value;}
} 

note: the value of your textbox1.text = sayre;

2.) Goto Form2 then Double click it. At the code type this.

// At your command button In Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 sForm1 = new Form1();
textBox1.Text = sForm1.CaptionText;
}
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115