Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Change A Button's Background Color

How can the background color of a button once another button is pressed?

What I have at the moment is:

ButtonToday.Background = Color.Red;

And it's not working.

like image 389
The Woo Avatar asked Feb 14 '11 10:02

The Woo


4 Answers

WinForm:

private void button1_Click(object sender, EventArgs e)
{
   button2.BackColor = Color.Red;
}

WPF:

private void button1_Click(object sender, RoutedEventArgs e)
{
   button2.Background = Brushes.Blue;
}
like image 61
Feroc Avatar answered Oct 23 '22 17:10

Feroc


In WPF, the background is not a Color, it is a Brush. So, try this for starters:

using System.Windows.Media;

// ....

ButtonToday.Background = new SolidColorBrush(Colors.Red);

More sensibly, though, you should probably look at doing this in your Xaml instead of in code.

like image 31
Dan Puzey Avatar answered Oct 23 '22 17:10

Dan Puzey


Code for set background color, for SolidColor:

button.Background = new SolidColorBrush(Color.FromArgb(Avalue, rValue, gValue, bValue));
like image 5
XiaoYao Avatar answered Oct 23 '22 17:10

XiaoYao


// WPF

// Defined Color
button1.Background = Brushes.Green;

// Color from RGB
button2.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));
like image 2
Volodymyr Sichka Avatar answered Oct 23 '22 15:10

Volodymyr Sichka