referencing a control on another form
-
I know that this is extremely simple. I want to display the text of a textBox on one form on the text of a label on another form. The code that I have works, but what gets displayed on form2.label is the text of form1.textBox at Design Time. Here is what I got. /// Code on the first form. private void button1_Click(object sender, System.EventArgs e) { Form2 form2 = new Form2(); form2.Show(); } /// Code on the second form. private void button1_Click(object sender, System.EventArgs e) { Form1 form1 = new Form1(); label1.Text = form1.textBox1.Text; }
-
I know that this is extremely simple. I want to display the text of a textBox on one form on the text of a label on another form. The code that I have works, but what gets displayed on form2.label is the text of form1.textBox at Design Time. Here is what I got. /// Code on the first form. private void button1_Click(object sender, System.EventArgs e) { Form2 form2 = new Form2(); form2.Show(); } /// Code on the second form. private void button1_Click(object sender, System.EventArgs e) { Form1 form1 = new Form1(); label1.Text = form1.textBox1.Text; }
-
I know that this is extremely simple. I want to display the text of a textBox on one form on the text of a label on another form. The code that I have works, but what gets displayed on form2.label is the text of form1.textBox at Design Time. Here is what I got. /// Code on the first form. private void button1_Click(object sender, System.EventArgs e) { Form2 form2 = new Form2(); form2.Show(); } /// Code on the second form. private void button1_Click(object sender, System.EventArgs e) { Form1 form1 = new Form1(); label1.Text = form1.textBox1.Text; }
kornstyle wrote: private void button1_Click(object sender, System.EventArgs e) { Form1 form1 = new Form1(); label1.Text = form1.textBox1.Text; } You are creating a new form everytime you try to get the text (but you never make it visible). That's why you get the text set at design time. You need to somehow get a reference to the existing
Form1
./// Code on the first form. private void button1_Click(object sender, System.EventArgs e) { Form2 form2 = new Form2(this); form2.Show(); } /// Code on the second form. Form _form1 = null; public Form2(Form form1) { _form1 = form1; } private void button1_Click(object sender, System.EventArgs e) { label1.Text = _form1.textBox1.Text; }
-- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!
-
kornstyle wrote: private void button1_Click(object sender, System.EventArgs e) { Form1 form1 = new Form1(); label1.Text = form1.textBox1.Text; } You are creating a new form everytime you try to get the text (but you never make it visible). That's why you get the text set at design time. You need to somehow get a reference to the existing
Form1
./// Code on the first form. private void button1_Click(object sender, System.EventArgs e) { Form2 form2 = new Form2(this); form2.Show(); } /// Code on the second form. Form _form1 = null; public Form2(Form form1) { _form1 = form1; } private void button1_Click(object sender, System.EventArgs e) { label1.Text = _form1.textBox1.Text; }
-- LuisR
Luis Alonso Ramos Intelectix - Chihuahua, Mexico Not much here: My CP Blog!