Click here to Skip to main content
12,952,443 members (41,053 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
I would like to change the color of a btn on click event. I tried this code but it didnt work:

btn12.backcolor=color.green;

Can anyone advice?
Posted 14-Jan-13 22:43pm
Updated 15-Jan-13 0:18am
digimanus59.8K
v2
Comments
Shanu2rick 15-Jan-13 4:44am
   
Its not the backcolor.
Its ForeColor.
Sandeep Mewara 15-Jan-13 13:53pm
   
Web or Winform?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

hi friend..

on btn click event.....
Btn1.BackColor = System.Drawing.Color.Red;
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Just check out the following, maybe it will help you:

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;
    }
  }
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

What exactly you mean it did not work,

If this is windows application written in C#,

button1.BackColor = Color.Green;


Just check, which event you are wiring up. Post some more code for us to see.
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

For Window Form application :

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



For Web Page :

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


and in button click event write :
protected void Button1_Click(object sender, EventArgs e)
       {
           if (!Isdefault)
           {
               Button1.BackColor = System.Drawing.Color.GreenYellow;
           }
       }
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Please refer following similar solved QA, you'll surely get some help out:
multiple buttons, one onclick event to change clicked button color[^]
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 6,289
CHill60 3,490
Maciej Los 3,103
Jochen Arndt 1,975
ppolymorphe 1,900


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100