The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 4,177 other subscribers

.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:


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:


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:


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

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: