.NET/C#: Chaning the ForeColor of a ReadOnly/Disabled TextBox (via: Stack Overflow)
Posted by jpluimers on 2014/12/24
Once every while you still do WinForms work, and bump into something you hadn’t bumped into before.
This time it was trying to set ForeColor = Color.Red
on a ReadOnly
TextBox
for displaying error messages:
- Using a TextBox means the user can still copy the text to the clipboard.
- Using a Red foreground draws enough attention (it’s was an app with a really busy user interface).
When setting a TextBox from ReadOnly = false to true sets the BackColor from SystemColors.Window (usually white) to SystemColors.Control (usually light grey), and leaves the ForeColor to SystemColors.WindowText (usually black).
Setting ForeColor = Color.Red (funny there is a plural in SystemColors but not in Color) it doesn’t display it as such:
To my surprise, the TextBox had ReadOnly text (you could copy, but not modify it), which showed with a a grey (SystemColors.Control) BackColor and a black (SystemColors.WindowText) ForeColor: the defaults for a ReadOnly TextBox, not using my ForeColor = Color.Red;
I vaguely remembered there was some odd way of solving this, but since I hadn’t written a blog article about it back then (somewhere around .NET 1.x or 2.0 I didn’t have a blog yet), I was glad that Cheetah posted this answer on StackOverflow:
Additionally, in order for ForeColor to be obeyed on a TextBox marked ReadOnly, you must explicitly set the BackColor. If you want to have it still use the default BackColor, you have to make the set explicit, as the designer is too smart for its own good here. It is sufficient to set the BackColor to its current value. […]
So: To make WinForms use the ForeColor, make sure to set the BackColor as well…
I modified his code a little bit:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ErrorTextBox.Text = "something went wrong"; | |
ErrorTextBox.BackColor = ErrorTextBox.ForeColor; |
To demonstrate the problem above, create a WinForms application with two TextBox controls and this code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Windows.Forms; | |
namespace ChartWindowsFormsApplication | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void Form1_Load(object sender, EventArgs e) | |
{ | |
textBox1.ReadOnly = false; | |
textBox2.ReadOnly = true; | |
SetTextBoxForeColorAndText(textBox1); | |
SetTextBoxForeColorAndText(textBox2); | |
} | |
private void SetTextBoxForeColorAndText(TextBox textBox) | |
{ | |
textBox.ForeColor = Color.Red; | |
textBox.Text = String.Format("ForeColor={0}, BackColor={1}", textBox.ForeColor, textBox.BackColor); | |
} | |
} | |
} |
So I committed this small extension method on the BeSharp.net repository to solve this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Windows.Forms; | |
namespace BeSharp.Windows.Forms | |
{ | |
public static class TextBoxBaseExtensions | |
{ | |
public static void SetForeColorOnReadOnly(this TextBoxBase target, Color color) | |
{ | |
target.ForeColor = color; | |
// http://stackoverflow.com/questions/276179/how-to-change-the-font-color-of-a-disabled-textbox/631983#631983 | |
if (target.ReadOnly) | |
target.BackColor = target.BackColor; | |
} | |
} | |
} |
–jeroen
via c# – How to change the font color of a disabled TextBox? – Stack Overflow.
Leave a Reply