views:

10603

answers:

3

How can I change the background and foreground colors of a WPF Textbox programatically in c#?

+14  A: 
textBox1.Background = Brushes.Blue;
textBox1.Foreground = Brushes.Yellow;
Timbo
Nice... hadn't seen the Brushes class before
James Hay
If we want to set a hex value to the color attribute , how it can be done??
Sauron
You could use something likeBrush brush = new SolidColorBrush( Color.FromRgb( r, g, b ) );
Timbo
There is also the much prettier `LinearGradientBrush` :)
BlueRaja - Danny Pflughoeft
+2  A: 

I take it you are creating the TextBox in xaml? In that case you need to give the text box a name. Then in the code behind you can then set the Background property using a variety of brushes. The simplest of which is the SolidColorBrush:

myTextBox.Background = new SolidColorBrush(Colors.White);
James Hay
A: 

Have you taken a look at Color.FromRgb?

Daniel