That won't work because it's the implementation of the OnPaint() method somewhere up the inheritance chain that decides the disabled color, and all controls have similar appearance when disabled for consistency.
You could override the OnPaint() method and do something like this:
Code:
public class CustomLabel : Label
{
public CustomLabel()
{
}
protected override void OnPaint(PaintEventArgs e)
{
if (!Enabled)
{
using (Label temp = new Label())
using (Bitmap bitmap = new Bitmap(Width, Height))
{
// Copy the relevant properties
temp.BackColor = BackColor;
temp.AutoSize = AutoSize;
temp.Size = Size;
temp.Text = Text;
temp.ForeColor = Color.Red; // the new disabled color
temp.DrawToBitmap(bitmap, temp.ClientRectangle); // (1) draw the temporary label to a bitmap, and then
e.Graphics.DrawImage(bitmap, Point.Empty); // (2) draw that bitmap instead of the label object
}
}
else
{
base.OnPaint(e);
}
}
}
A quicker approach would be to use this.SetStyle(ControlStyles.UserPaint, true); as in the following example :
CustomLabel.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace COD
{
public class CustomLabel : Label
{
public CustomLabel()
{
this.SetStyle(ControlStyles.UserPaint, true); //Call in constructor, Use UserPaint
}
protected override void OnPaint(PaintEventArgs e)
{
if (!Enabled)
{
SolidBrush drawBrush = new SolidBrush(Color.DarkSlateBlue); //Choose colour
e.Graphics.DrawString(Text, Font, drawBrush, 0f, 0f); //Dra whatever text was on the label
}
else
{
base.OnPaint(e); //Default Forecolours
}
}
}
}
Re: How to change the label disabled forecolor - Counterexample
@HanneSThEGreaT:
That approach is a bit quicker, and I've considered it, but there's a slight problem: text drawn from the string may not render exactly the same way as the corresponding text on the label - there could be a slight offset in placement and maybe even font character width (even if it's the same font), depending on the exact way the Graphics class renders it.
I attached a counterexample project which demonstrates both techniques on two font sizes (I just added a large font version where that the effect is more visible).
Last edited by TheGreatCthulhu; November 12th, 2012 at 06:26 PM.
Re: How to change the label disabled forecolor - Counterexample
Originally Posted by TheGreatCthulhu
@HanneSThEGreaT:
That approach is a bit quicker, and I've considered it, but there's a slight problem: text drawn from the string may not render exactly the same way as the corresponding text on the label - there could be a slight offset in placement and maybe even font character width (even if it's the same font), depending on the exact way the Graphics class renders it.
I attached a counterexample project which demonstrates both techniques on two font sizes (I just added a large font version where that the effect is more visible).
Interesting. Just proves you can teach an old dog new tricks!
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.