Set background color of WPF Textbox in C# code
Asked Answered
C

8

208

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

Clarabelle answered 11/6, 2009 at 7:56 Comment(0)
S
378
textBox1.Background = Brushes.Blue;
textBox1.Foreground = Brushes.Yellow;

WPF Foreground and Background is of type System.Windows.Media.Brush. You can set another color like this:

using System.Windows.Media;

textBox1.Background = Brushes.White;
textBox1.Background = new SolidColorBrush(Colors.White);
textBox1.Background = new SolidColorBrush(Color.FromArgb(0xFF, 0xFF, 0, 0));
textBox1.Background = System.Windows.SystemColors.MenuHighlightBrush;
Schilling answered 11/6, 2009 at 8:6 Comment(4)
If we want to set a hex value to the color attribute , how it can be done??Clarabelle
You could use something like Brush brush = new SolidColorBrush( Color.FromRgb( r, g, b ) );Schilling
There is also the much prettier LinearGradientBrush :)Randolph
Be sure to include System.Windows.Media.Milli
I
120

If you want to set the background using a hex color you could do this:

var bc = new BrushConverter();

myTextBox.Background = (Brush)bc.ConvertFrom("#FFXXXXXX");

Or you could set up a SolidColorBrush resource in XAML, and then use findResource in the code-behind:

<SolidColorBrush x:Key="BrushFFXXXXXX">#FF8D8A8A</SolidColorBrush>
myTextBox.Background = (Brush)Application.Current.MainWindow.FindResource("BrushFFXXXXXX");
Infrastructure answered 7/11, 2011 at 8:9 Comment(2)
It is much preferable to use (System.Windows.Media.Brush)Application.Current.FindResource("BrushFFXXXXX"); as your application will not throw a threading exception if it is upgraded to use multiple dispatcher threads in the future.Viscount
Where should <SolidColorBrush x:Key="BrushFFXXXXXX">#FF8D8A8A</SolidColorBrush> be declared? When I try do so inside <Window x:Class ... I get error: "The property 'Content' is set more than once"Eternize
H
24

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);
Hockett answered 11/6, 2009 at 8:6 Comment(0)
M
7

You can convert hex to RGB:

string ccode = "#00FFFF00";
int argb = Int32.Parse(ccode.Replace("#", ""), NumberStyles.HexNumber);
Color clr = Color.FromArgb(argb);
Madelina answered 30/4, 2012 at 3:3 Comment(1)
System.Windows.Media.Color FromArgb is accepting byte a, byte r, byte g, byte b, not intUnderhand
M
6

You can use hex colors:

your_contorl.Color = DirectCast(ColorConverter.ConvertFromString("#D8E0A627"), Color)
Madelina answered 30/4, 2012 at 3:40 Comment(0)
M
4

I know this has been answered in another SOF post. However, you could do this if you know the hexadecimal.

textBox1.Background = (SolidColorBrush)new BrushConverter().ConvertFromString("#082049");
Maniemanifest answered 24/2, 2021 at 20:8 Comment(0)
C
1

Have you taken a look at Color.FromRgb?

Celaeno answered 29/4, 2010 at 21:43 Comment(0)
D
0

BrushConverter bc = new BrushConverter();

textName.Background = (Brush)bc.ConvertFrom("#FF7BFF64");

buttonName.Foreground = new SolidColorBrush(Colors.Gray);

Reference Here https://www.it-mure.jp.net/ja/c%23/c%EF%BC%83%E3%82%B3%E3%83%BC%E3%83%89%E3%81%A7wpf%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E3%83%9C%E3%83%83%E3%82%AF%E3%82%B9%E3%81%AE%E8%83%8C%E6%99%AF%E8%89%B2%E3%82%92%E8%A8%AD%E5%AE%9A%E3%81%99%E3%82%8B/957683208/

Dreamland answered 26/8, 2021 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.