A strange Problem

tot2ivn

Newcomer
Joined
Dec 15, 2004
Messages
4
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 ?
 
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 !
:(
 
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.
 
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
 
Last edited by a moderator:
Try changing form2's code to be
C#:
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
C#:
Form2 obj_f2 = new Form2(this);
obj_f2.ChangeText();

like the tutorial shows.
 
Back
Top