Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms: How do you change the font color for a disabled label

Tags:

winforms

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.

like image 390
mistrmark Avatar asked Sep 25 '08 21:09

mistrmark


3 Answers

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)?

like image 124
Austin Salonen Avatar answered Sep 24 '22 04:09

Austin Salonen


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.

like image 31
Andrew Avatar answered Sep 25 '22 04:09

Andrew


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);
like image 29
Richard Morgan Avatar answered Sep 24 '22 04:09

Richard Morgan