Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
Edited 15-Jan-13 0:18am
v2
Comments
Shanu2rick at 15-Jan-13 4:44am
   
Its not the backcolor.
Its ForeColor.
Sandeep Mewara at 15-Jan-13 13:53pm
   
Web or Winform?
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  
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 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 at 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 at 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 at 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 at 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 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 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 7

hi friend..
 
on btn click event.....
Btn1.BackColor = System.Drawing.Color.Red;
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 363
1 Sergey Alexandrovich Kryukov 286
2 RyanDev 75
3 nv3 60
4 PhilLenoir 60
0 Sergey Alexandrovich Kryukov 6,691
1 OriginalGriff 6,119
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2014
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