tot2ivn Posted December 16, 2004 Posted December 16, 2004 I created 2 two classes: Form1 and Form2 under the same namespace. + Form1.cs was a dialog. + Form2.cs was just a normal class. In Form1 I created a button, with the public variable, named button1. In Form2 I wrote a method : public void ChangeBackColor() { Form1 obj = new Form1(); obj.button1.BackColor = Color.Blue; MessageBox.Show(obj.button1.BackColor.ToString()); } Then in Form1 I also wrote a method to call the ChangeBackColor() from Form2 : public void Click() { Form2 obj_f2 = new Form2(); obj_f2.ChangeBackColor(); } The compiling process generated 0 errors and the program still ran. But when I click a button to call Click() from Form1 It just appeared a Message Box showing the Default Color of button1 the Color did not change. Anybody can help me ?? That seemed simple but I still can not find the reason WHY ? Quote
Administrators PlausiblyDamp Posted December 16, 2004 Administrators Posted December 16, 2004 With the line Form1 obj = new Form1(); you are creating a new instance of form1 not refering to the existing one. this is probably worth a read. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
tot2ivn Posted December 16, 2004 Author Posted December 16, 2004 Thanks for your reply but If it doesn't refer to that existing Form1. How come the MessageBox still works normally ?? I still can retrieve data & value from Form1.cs but can NOT MODIFY them ! :( Quote
Administrators PlausiblyDamp Posted December 16, 2004 Administrators Posted December 16, 2004 Any chance you could attach the code in question (no binaries though please), as I cannot see how it can return the existing values for form1 when you are creating a new instance of Form1 in the procedure. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
tot2ivn Posted December 18, 2004 Author Posted December 18, 2004 (edited) Here is my program. Please help me change (modify) a value of Form1.cs through a method in Form2.cs Coz I'm just a beginner in C#.NET Thanks a lot !! :D Edited December 18, 2004 by PlausiblyDamp Quote
Administrators PlausiblyDamp Posted December 18, 2004 Administrators Posted December 18, 2004 Try changing form2's code to be private Form1 f; public Form2(Form1 f1) { f=f1; } public void ChangeText() { MessageBox.Show("Text of button1 is " + f.button1.Text.ToString()); f.button1.Text = "Loving ya"; } and form1's code to be Form2 obj_f2 = new Form2(this); obj_f2.ChangeText(); like the tutorial shows. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
tot2ivn Posted December 19, 2004 Author Posted December 19, 2004 (edited) THanks Mr.SuperMod, I'll try this solution ! :D Edited December 19, 2004 by tot2ivn Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.