11
votes

I am trying to set the disabled font characteristics for a Label Control. I can set all of the Font characteristics (size, bold, etc), but the color is overridden by the default windows behavior which seems to be one of these two colors:

  • If background color is transparent then ForeColor is same as TextBox disabled Color.
  • If background color is set to anything else, ForeColor is a Dark Gray color.

The image below demonstrates the behavior -- Column 1 is Labels, Column 2 is TextBoxs, and Column 3 is ComboBoxes.

alt text

Edit -- Explaining the image: The first two rows are default styles for a label, textbox, and combobox. In the second two rows, I set the Background color to Red and Foreground to White. The disabled font style handling by Microsoft is inconsistent.

6

6 Answers

2
votes

Have you tried implementing the EnabledChanged event? Or are you looking for more of a "styles" property on the control (as far as I know, they don't exist)?

2
votes

For the textbox, you can set the readonly property to true while keeping the control enabled. You can then set the BackColor and ForeColor property to whatever you like. The user will still be able to click on the control and have a blinking cursor, but they won't be able to edit anything.

Not sure if this extrapolates out to other control types like combo boxes or whatnot as I haven't had a chance to experiment yet, but it's worth a shot.

1
votes

Take a look at the ControlPaint.DrawStringDisabled method; it might be something helpful. I've used it when overriding the OnPaint event for custom controls.

ControlPaint.DrawStringDisabled(g, this.Text, this.Font, Color.Transparent,
                new Rectangle(CustomStringWidth, 5, StringSize2.Width, StringSize2.Height), StringFormat.GenericTypographic);
0
votes

Why is this an issue?

I would personally let windows handle it. People are used to disabled items looking a certain way, so if you start trying to change every aspect of the way they look, you might start confusing your users.

0
votes

You will probably need to override the Paint event. All toolkits I've used so far have the same problem when the control is disabled. Just guess they let windows do the drawing of the text. As for the labels, well they are not an standard control, and that's why they are working.

0
votes

I overrode the OnPaint method of my control with the OnPaint method below. I pasted the entire control class to make it easy to copy.

public partial class NewLabel : Label
{
    public NewLabel()
    {
        InitializeComponent();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        TextRenderer.DrawText(e.Graphics, this.Text.ToString(), this.Font, ClientRectangle, ForeColor);
    }

}