Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 ?

Posted

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 !

:(

Posted (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 by PlausiblyDamp
  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...