Change second form control text
-
hello i have 2 forms . how do i change form2 button control text from form1? what interface do i need to add? thanks Have a nice Day
There are three steps involved in using form to form communication: 1. Create a reference to form2 from form1. 2. Set your reference equal to your instance. 3. Set your control to public. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Since we are dealing with objects here, you have to make sure they know about each other (at least form1 knows about form2). The way to do this is to create a reference to form2 from form1:
public class Form1 : System.Windows.Forms.Form { private Form2 form2 = new Form2(); ...}
2. Setting your reference to the instance of form2. Something like this:private void button1_Click(object sender, System.EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); //This is where you would set you reference equal to your instance. form2 = frm2; }
3. Set the control to public (in form2):public class Form2 : System.Windows.Forms.Form { public System.Windows.Forms.Button button1; ...}
NOTE: your autogenerated code should already have a definition of the button1 but it will be set to private, what you are doing here is changing it to public. Any question let me know. Peter Corcoran peter@corcoranp.com www.corcoranp.com -
There are three steps involved in using form to form communication: 1. Create a reference to form2 from form1. 2. Set your reference equal to your instance. 3. Set your control to public. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1. Since we are dealing with objects here, you have to make sure they know about each other (at least form1 knows about form2). The way to do this is to create a reference to form2 from form1:
public class Form1 : System.Windows.Forms.Form { private Form2 form2 = new Form2(); ...}
2. Setting your reference to the instance of form2. Something like this:private void button1_Click(object sender, System.EventArgs e) { Form2 frm2 = new Form2(); frm2.Show(); //This is where you would set you reference equal to your instance. form2 = frm2; }
3. Set the control to public (in form2):public class Form2 : System.Windows.Forms.Form { public System.Windows.Forms.Button button1; ...}
NOTE: your autogenerated code should already have a definition of the button1 but it will be set to private, what you are doing here is changing it to public. Any question let me know. Peter Corcoran peter@corcoranp.com www.corcoranp.com