My form 2 is opened by form 1, like this:
Form2 fm2 = new Form2();
fm2.Show();
So now, can my form 2 change the text in the TextBox of the current form 1?
I have tried using the following:
Form1 fm1 = new Form1();
fm1.textBox_A.Text = "123";
But it is not working.
CodePudding user response:
Oh, I just found a way to do what I want.
for form2:
Form1 fm1;
public Form2(Form1 _Form1)
{
fm1 = _Form1;
InitializeComponent();
...
}
Then form1:
private void Form1_Load(object sender, EventArgs e)
{
Form2 fm2 = new Form2(this);
fm2.Show();
}
public string ChangeText
{
get { return this.textBox_A.Text; }
set
{
this.textBox_A.Text = value;
}
}
So now I can use the following in form 2 to change the Text in current Form1 by Form2:
fm1.ChangeText= "YAAAAAAAAAAAAAAAA";
CodePudding user response:
in first step you should add new constructor in Form2
System.Windows.Forms.Form fm1;
public Form2(System.Windows.Forms.Form frm1)
{
this.fm1 = frm1;
InitializeComponent();
}
then just write this
fm1.textBox_A.Text = "123";