By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,884 Members | 949 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,884 IT Pros & Developers. It's quick & easy.

How to change form2 color on button click , calling the changeColor method of form1:

P: 18
Hi,

I have two forms. Let say, form1 and form2. in form1 class, i have method ChangeColor() and i am calling this method in button click event of form2. But it is not changing the color of form2.

Please find the below code:
Below code is for form1, where on click of Button1, form2 is opening.

Expand|Select|Wrap|Line Numbers
  1. public partial class Form1 : Form
  2.     {
  3.         public Form1()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.  
  8.  
  9.         private void button1_Click(object sender, EventArgs e)
  10.         {
  11.             Form2 fm2 = new Form2();
  12.             fm2.Show();
  13.         }
  14.         public void changecolor()
  15.         {
  16.  
  17.             this.BackColor = Color.Red;
  18.             this.Refresh();
  19.         }
  20.  
  21.  
  22.     }
Below code is for form2, where on click of button2, changecolor() i am calling. But unable to change the color of the form2.

Expand|Select|Wrap|Line Numbers
  1. public partial class Form2 : Form
  2.     {
  3.         public Form2()
  4.         {
  5.             InitializeComponent();
  6.         }
  7.  
  8.         private void button1_Click(object sender, EventArgs e)
  9.         {
  10.               Form1 frm1=new Form1();
  11.               frm1.changecolor();
  12.         }
  13.     }
Please suggest.

Thanks,
Snehasis
Nov 14 '11 #1
Share this Question
Share on Google+
1 Reply


P: 24
Pass an object to changecolor() of form1 then make changes to changecolor() like this in form1.
Expand|Select|Wrap|Line Numbers
  1.   public void changecolor(Form2 frm2) 
  2.         { 
  3.  
  4.             frm2.BackColor = Color.Red; 
  5.             frm2.Refresh(); 
  6.         } 
  7.  
Nov 14 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.