passing data from Form2 to Form1
Hi,
I have 2 forms , Form1 and Form2. When I click on the "Ok" buttom of the Form 1, it creates an instance of Form2 as below
Form2 newform = new Form2();
newform.Show();
Form2 has a textbox which accepts input from user. I am able to get the Form2 and its textbox, but I am having problems of passing data from Form2 to Form1. Sending data from Form1 to Form2 is not a problem. But here it is the reverse case. How do i pass the data in textbox of Form2 to Form1 ??
Re: passing data from Form2 to Form1
set the "Modifiers" property of the textbox to "Public", then...
Code:
newform.ShowDialog();
string dataFromForm = newform.textBox1.Text;
newform.Dispose();
1 Attachment(s)
Re: passing data from Form2 to Form1
Quote:
Originally Posted by sangfroid
Hi,
I have 2 forms , Form1 and Form2. When I click on the "Ok" buttom of the Form 1, it creates an instance of Form2 as below
Form2 newform = new Form2();
newform.Show();
Form2 has a textbox which accepts input from user. I am able to get the Form2 and its textbox, but I am having problems of passing data from Form2 to Form1. Sending data from Form1 to Form2 is not a problem. But here it is the reverse case. How do i pass the data in textbox of Form2 to Form1 ??
Use delegate and events to send info from form2 to form1
Have a look into the attached file....
Hope this is what you are looking for..
MMH...
Re: passing data from Form2 to Form1
It depends on how you want Form2 to behave.
If you want it to behave like a FileOpen dialog (i.e. stops all input into Form1 until it's closed) use eclipsed4utoo's suggestion but with one change :
DONT MAKE MEMBER VARIABLES PUBLIC
This breaks encapsulation. You should instead have a public property which returns the text in the text box e.g.
Code:
public class Form2 : Form
{
private TextBox _textBox;
public string TextBoxText
{
get
{
return _textBox.Text;
}
}
}
However if you want Form2 to open and stay open whilst the user does things with Form1 then you should use MMH's suggestion of having an event.
Darwen.
Re: passing data from Form2 to Form1
You can also send data through the constructor of Form2...
Code:
Form2 form2 = new Form2("hi", "sup?", "bye");
using(form2)
{
form2.ShowDialog();
}
Re: passing data from Form2 to Form1
Quote:
Originally Posted by Talikag
You can also send data through the constructor of Form2...
Code:
Form2 form2 = new Form2("hi", "sup?", "bye");
using(form2)
{
form2.ShowDialog();
}
He wants to send data from form2 to form1 and not vice-versa.