Click here to Skip to main content
15,884,836 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to change the color of a btn on click event. I tried this code but it didnt work:

C#
btn12.backcolor=color.green;

Can anyone advice?
Posted
Updated 15-Jan-13 0:18am
v2
Comments
Shanu2rick 15-Jan-13 4:44am    
Its not the backcolor.
Its ForeColor.
Sandeep Mewara 15-Jan-13 13:53pm    
Web or Winform?

Please refer following similar solved QA, you'll surely get some help out:
multiple buttons, one onclick event to change clicked button color[^]
 
Share this answer
 
For Window Form application :

C#
private void button3_Click(object sender, EventArgs e)
        {
            button3.BackColor = System.Drawing.Color.Black;
        }



For Web Page :

On Page level declare static variable:
C#
static bool Isdefault= true;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Isdefault = false;
            }
        }


and in button click event write :
C#
protected void Button1_Click(object sender, EventArgs e)
       {
           if (!Isdefault)
           {
               Button1.BackColor = System.Drawing.Color.GreenYellow;
           }
       }
 
Share this answer
 
v2
Just check out the following, maybe it will help you:

C#
public void single_button_Click(object sender, EventArgs e)
  {
    Button btn = (Button)sender;
    if (btn.BackColor == Color.Red)
    {
      btn.BackColor = SystemColors.Control;
    }
    else if (btn.BackColor == SystemColors.Control)
    {
      btn.BackColor = Color.Red;
    }
  }
 
Share this answer
 
v2
Comments
S.Rajendran from Coimbatore 15-Jan-13 8:59am    
I used in the asp.net c# web page. It is not working. It gives an error stating that:

The name 'color' and 'SystemColors' does not exist in the current context
shiny13 15-Jan-13 9:04am    
You didn't specify so I thought maybe its for winform. Include System.Drawing.Color namespace at the top and you will get some colors.
S.Rajendran from Coimbatore 15-Jan-13 10:48am    
I uncluded 'using System.Drawing.Color' at the top. It says :"using namespace directive can only be applied to namespaces; 'System.Drawing.Color' is a type not a namespace"
shiny13 16-Jan-13 2:02am    
ok then maybe try using color like this:
string hexValue = "#000000"; // You do need the hash
Color colour = System.Drawing.ColorTranslator.FromHtml(hexValue); // Yippee
Hi,

You can do it in various ways. Check some of the links here.

Change Background Color of ASP.NET Button using jQuery
Changing color of jQuery UI Buttons
C# Change A Button's Background Color

These links will guide you.
Thanks
 
Share this answer
 
If its a windows based application then,

change the property of your button's flat style to "Standard" ane on click write the below:
button1.BackColor = Color.Red;

it will work fine.
 
Share this answer
 
What exactly you mean it did not work,

If this is windows application written in C#,

C#
button1.BackColor = Color.Green;


Just check, which event you are wiring up. Post some more code for us to see.
 
Share this answer
 
v2
hi friend..

on btn click event.....
C#
Btn1.BackColor = System.Drawing.Color.Red;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900