How to show form2 textbox data in form1 textbox
I have 2 forms form1 and form2. First when you run the application form1 will open. In form1 there is one text box and one button. Do not enter any text in that textbox. Please just click on the button. After clicking the button form2 should be displayed. But, do not close form1. In form2 also there is one text box and one button. Now enter some text in the form2 textbox and click the button. The text should be displayed in the form1 textbox.
Hirendra SisodiyaPosted Mar 25, 2010, 1:29 AM
you can use also this code on button1 click of form2 , in this case there is no need to make construstor...
private void button1_Click(object sender, EventArgs e)
{
foreach (Form frm in Application.OpenForms)
{
if (frm.Name == "Form1")
{
Form1 temp = (Form1)frm;
temp.textBox1.Text = "Hello";
Application.DoEvents();
}
}
}
thanks
Please mark as answere if you like my answere
ramesh pPosted Mar 24, 2010, 3:38 AM
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
}
public partial class Form2 : Form
{
Form _frm;
public Form2(Form frm)
{
_frm=frm;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 objMain = (Form1)_frm ;
objMain.textBox1.Text = "Hello";
}
}
In this technique, i pass the form1 to Form2 in constructor, then create an object of existing Form1 and change there value