How to change the label disabled forecolor
As the title suggests,
how to change the standard (default) forecolor when the label is disabled.
I came up with the following:
Code:
namespace COD
{
class CustomLabel : System.Windows.Forms.Label
{
private bool _enabled = true;
protected override bool Enabled
{
get
{
return _enabled;
}
set
{
_enabled = value;
if (_enabled)
this.ForeColor = Color.FromKnownColor(KnownColor.Yellow);
else
this.ForeColor = Color.FromKnownColor(KnownColor.Red);
}
}
}
}
but it is not working ....
regards,
G.
Re: How to change the label disabled forecolor
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);
}
}
}
1 Attachment(s)
Re: How to change the label disabled forecolor
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
}
}
}
}
YOURFORM.css :
Code:
private void button1_Click(object sender, EventArgs e)
{
customLabel1.Enabled = false; //Disabled
}
private void button2_Click(object sender, EventArgs e)
{
customLabel1.Enabled = true; //Enabled
}
Easy! :D
Have a look at the attachment.
Hannes
Re: How to change the label disabled forecolor
Check out moving to WPF where you have increased control over customizing all the visual elements.
1 Attachment(s)
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).
Re: How to change the label disabled forecolor - Counterexample
Quote:
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! :thumb: :D