[RESOLVED] [2005] Custom Colors on Disabled Control
I have this code for making a custom disabled control that allows you to change the background and foreground colors on a TextBox...
Code:
Option Strict On
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class TextboxExtended
Inherits System.Windows.Forms.TextBox
' Color used for background and foreground when
' control is diabled
Private _BackColorDisabled As Color = Color.LightYellow
Private _ForeColorDisabled As Color = Color.Black
<Category("Appearance"), _
Description("Sets the BackColor to Non-Standard when Disabled"), _
DefaultValue(GetType(Color), "Light Yellow")> _
Public Property BackColorDisabled() As Color
Get
Return _BackColorDisabled
End Get
Set(ByVal Value As Color)
_BackColorDisabled = Value
End Set
End Property
<Category("Appearance"), _
Description("Sets the BackColor to Non-Standard when Disabled"), _
DefaultValue(GetType(Color), "Black")> _
Public Property ForeColorDisabled() As Color
Get
Return _ForeColorDisabled
End Get
Set(ByVal Value As Color)
_ForeColorDisabled = Value
End Set
End Property
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
' Set the TextBrush with the color to paint disabled control text
Dim TextBrush As SolidBrush = New SolidBrush(Me._ForeColorDisabled)
' Is the Control Disabled
If Not Me.Enabled Then
' The control is diabled use BackColorDisabled porperty for background
Dim BackBrush As New SolidBrush(_BackColorDisabled)
e.Graphics.FillRectangle(BackBrush, 0.0F, 0.0F, Me.Width, Me.Height)
End If
' Panint the text with the ForeColorDisabled color
e.Graphics.DrawString(Me.Text, Me.Font, TextBrush, 0.0F, 0.0F)
End Sub
Protected Overrides Sub OnEnabledChanged(ByVal e As System.EventArgs)
MyBase.OnEnabledChanged(e)
If Not Me.Enabled Then
Me.SetStyle(ControlStyles.UserPaint, True)
Else
Me.SetStyle(ControlStyles.UserPaint, False)
End If
End Sub
End Class
Problem is that when it is fired the properties I've set in the regular textbox properties are ignored, specifically the textalign property. Why is this happening and how can I fix it?
Re: [2005] Custom Colors on Disabled Control
its ignoring it because you are drawing the string at point 0,0 which is always the left top corner of the control.
Here I modified your class (made some additional changes as well)
Code:
Option Strict On
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Public Class TextboxExtended
Inherits System.Windows.Forms.TextBox
' Color used for background and foreground when
' control is diabled
Private _BackColorDisabled As Color = Color.LightYellow
Private _ForeColorDisabled As Color = Color.Black
'HOLDS ALIGNMENT FOR STRING TO BE DRAWN
Private _StringFormatAlignment As New StringFormat
'BRUSH USED TO PAINT TEXT
Private _TextBrush As New SolidBrush(_ForeColorDisabled)
'BRUSH USED TO PAINT BACKCOLOR
Private _BackBrush As New SolidBrush(_BackColorDisabled)
<Category("Appearance"), _
Description("Sets the BackColor to Non-Standard when Disabled"), _
DefaultValue(GetType(Color), "Light Yellow")> _
Public Property BackColorDisabled() As Color
Get
Return _BackColorDisabled
End Get
Set(ByVal Value As Color)
_BackColorDisabled = Value
_BackBrush.Color = Value
End Set
End Property
<Category("Appearance"), _
Description("Sets the BackColor to Non-Standard when Disabled"), _
DefaultValue(GetType(Color), "Black")> _
Public Property ForeColorDisabled() As Color
Get
Return _ForeColorDisabled
End Get
Set(ByVal Value As Color)
_ForeColorDisabled = Value
_TextBrush.Color = Value
End Set
End Property
Protected Overrides Sub OnTextAlignChanged(ByVal e As System.EventArgs)
Select Case MyBase.TextAlign
Case HorizontalAlignment.Left
_StringFormatAlignment.Alignment = StringAlignment.Near
Case HorizontalAlignment.Center
_StringFormatAlignment.Alignment = StringAlignment.Center
Case HorizontalAlignment.Right
_StringFormatAlignment.Alignment = StringAlignment.Far
End Select
MyBase.OnTextAlignChanged(e)
End Sub
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
'if not enabled, paint custom, otherwise just call
' base class OnPaint so control will do it automatically
If Not Me.Enabled Then
'Paint background color
e.Graphics.FillRectangle(_BackBrush, 0.0F, 0.0F, Me.Width, Me.Height)
' Paint the text with the ForeColorDisabled color
e.Graphics.DrawString(Me.Text, Me.Font, _TextBrush, e.ClipRectangle, _StringFormatAlignment)
Else
MyBase.OnPaint(e)
End If
End Sub
Protected Overrides Sub OnEnabledChanged(ByVal e As System.EventArgs)
MyBase.OnEnabledChanged(e)
If Not Me.Enabled Then
Me.SetStyle(ControlStyles.UserPaint, True)
Else
Me.SetStyle(ControlStyles.UserPaint, False)
'setting the font again here fixes bug
'where font looks bold even though its not
Me.Font = Me.Font
'invalidate control so it repaints.
'If you don't sometimes the text is
'invisible until the control gets focus
Me.Invalidate()
End If
End Sub
End Class
For one thing, I trimmed down the paint routine to not create new brush objects everytime. Being that the paint routine fires a lot for a control, there is no point in creating objects everytime in the routine. Create them once, and modify their color properties when the colors are changed in the set portion of their properties. Also there is (what I think is) a bug when you custom draw in a textbox, where the font can appear bold even though its not, when you turn userpaint off. This can be fixed by setting the font object to itself, and invalidating the control (as seen in the OnEnabledChanged sub in your code).
Also I used a StringFormat object when calling the DrawString method to set the correct alignment for the text. Then added an overrides sub OnTextAlignChanged, which will set the alignment to the correct one based on the user setting.
Re: [2005] Custom Colors on Disabled Control
Thank you very much...that fixed the alignment problem.
I'm still having the problem with the font looking bold though...even with your fix.
I can't take credit or blame for the first class I posted on here, I found it somewhere else (experts exchange).
1 Attachment(s)
Re: [2005] Custom Colors on Disabled Control
I am not having any issue.
I attached the test project I used.
Re: [RESOLVED] [2005] Custom Colors on Disabled Control
My extended Textbox class is identical to yours...though I still get the "bold" type when enabling the control. This is not a big deal and the project will work with it this way. Now I just have to port this functionality over to a masked textbox and a date time picker!
Other than that it works!
Thanks
Re: [RESOLVED] [2005] Custom Colors on Disabled Control
I figured out the "bold" issue. I have changed the font style and size. If I change it back to the default this issue doesn't occur.
3 Attachment(s)
Re: [RESOLVED] [2005] Custom Colors on Disabled Control
I have copied this solution to be used on multiple controls.
DateTimePicker - unchecked boxes show as blank with the DisabledBackColor
MaskedTextBox
TextBox