Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to change the color of the button when I click on it in windows application of C#.. Anybody can help me?
Posted
Updated 18-Dec-12 2:19am
v3

Hello

In addition to the answers if you want to change it beautifully:
Visual Studio C# Windows Forms… changing button color?
 
Share this answer
 
Comments
[no name] 18-Dec-12 8:40am    
really good link.. :)
Shahin Khorshidnia 19-Dec-12 0:19am    
Thank you!
Hi,

Add a Click event handler to all your buttons:
C#
public Form1()
{
     InitializeComponent();
     button1.Click += new EventHandler(allButtons_Click);
     button2.Click += new EventHandler(allButtons_Click);
     button3.Click += new EventHandler(allButtons_Click);
     // do this for all your buttons
}

Then, create the allButtons_Click function:
C#
void allButtons_Click(object sender, EventArgs e)
{
     Button senderBtn = sender as Button;
     senderBtn.ForeColor = Color.Red; // set the fore color of the button
     senderBtn.BackColor = Color.Orange; // set the back color of the button
}

Hope this helps.
 
Share this answer
 
Comments
Shahin Khorshidnia 18-Dec-12 8:31am    
Or:
var buttons = new Button[]{button1,button2,button3};
foreach(var button in buttons)
{
button.Click += new EventHandler(AllButtonsClick);
}
C#
protected void Button1_Click(object sender, EventArgs e)
{
    if(Button.BackColor != Color.Red)
        Button1.BackColor = Color.Red;
    else
        Button1.BackColor = Color.Blue;
}
 
Share this answer
 
Comments
Shahin Khorshidnia 18-Dec-12 8:22am    
Or:

Button1.BackColor = (Button.BackColor == Color.Red)? Color.Blue:Color.Red;
[no name] 18-Dec-12 8:25am    
Its up to you, how you do it... :)
Shahin Khorshidnia 19-Dec-12 0:20am    
But recommended :)

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