Click here to Skip to main content
Sign Up to vote bad
good
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:43
Edited 15 Jan '13 - 0:18
digimanus22.7K

Comments
Shanu2rick - 15 Jan '13 - 4:44
Its not the backcolor. Its ForeColor.
Sandeep Mewara - 15 Jan '13 - 13:53
Web or Winform?

7 solutions

Please refer following similar solved QA, you'll surely get some help out:
multiple buttons, one onclick event to change clicked button color[^]
  Permalink  
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  
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  
Comments
S.Rajendran from Coimbatore - 15 Jan '13 - 8:59
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:04
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:48
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:02
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
  Permalink  
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  
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  
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 259
1 OriginalGriff 220
2 Mahesh Bailwal 159
3 CPallini 143
4 Maciej Los 140
0 Sergey Alexandrovich Kryukov 10,214
1 OriginalGriff 7,819
2 CPallini 4,181
3 Rohan Leuva 3,522
4 Maciej Los 3,089


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 15 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid