Interaction between a Form and a Class
-
I have a form where i call a class with and start a subroutine Class1 Class1 = Class1(); Class1.StartSub(); In my Mainform from where i call the class there is for example a label called "label1". Now i want to change the label1 from within the Class Subroutine ... I tried following: in the MainForm (Form1) set the label from private to public and then in the Class: Form1 Form1 = new Form1(); Form1.label1.text = "test"; It compiles without any error but it doesn't change the label. Thanks for you help!
-
I have a form where i call a class with and start a subroutine Class1 Class1 = Class1(); Class1.StartSub(); In my Mainform from where i call the class there is for example a label called "label1". Now i want to change the label1 from within the Class Subroutine ... I tried following: in the MainForm (Form1) set the label from private to public and then in the Class: Form1 Form1 = new Form1(); Form1.label1.text = "test"; It compiles without any error but it doesn't change the label. Thanks for you help!
When you created the new Form1(), you created a whole new instantiation of that form in memory, so you changed HIS label and not the one that is visible. You could tackle that a couple of ways. You could put in the Class1's constructor an argument for a label like this Class1(label FormsLabelIWantToChange) { FormsLabelIWantToChange = "test"; } Another way to do it (I think) would be to make Form1's label member s public static, then as you are in the same namespace you could do something like this in your class: Form1.label1.text = "test";
There are only 10 types of people in this world....those that understand binary, and those that do not.