The Woo
The Woo

Reputation: 18645

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.

Upvotes: 39

Views: 345256

Answers (7)

Bilal Boukerma
Bilal Boukerma

Reputation: 1

this.button2.BaseColor = System.Drawing.Color.FromArgb(((int)(((byte)(29)))), ((int)(((byte)(190)))), ((int)(((byte)(149)))));

Upvotes: 0

Core_F
Core_F

Reputation: 3442

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;
}

Upvotes: 84

Volodymyr Sichka
Volodymyr Sichka

Reputation: 561

// WPF

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

// Color from RGB
button2.Background = new SolidColorBrush(Color.FromArgb(255, 0, 255, 0));

Upvotes: 2

Jiby Matthews
Jiby Matthews

Reputation: 29

I doubt if any of those should work. Try: First import the namespace in the beginning of the code page as below.

using System.Drawing;

then in the code.

Button4.BackColor = Color.LawnGreen;

Hope it helps.

Upvotes: 0

XiaoYao
XiaoYao

Reputation: 51

Code for set background color, for SolidColor:

button.Background = new SolidColorBrush(Color.FromArgb(Avalue, rValue, gValue, bValue));

Upvotes: 5

Viper
Viper

Reputation: 11

I had trouble at first with setting the colors for a WPF applications controls. It appears it does not include System.Windows.Media by default but does include Windows.UI.Xaml.Media, which has some pre-filled colors.

I ended up using the following line of code to get it to work:

grid.Background.SetValue(SolidColorBrush.ColorProperty, Windows.UI.Colors.CadetBlue);

You should be able to change grid.Background to most other controls and then change CadetBlue to any of the other colors it provides.

Upvotes: 1

Dan Puzey
Dan Puzey

Reputation: 34200

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.

Upvotes: 16

Related Questions