Option Strict On

Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel

Public Class extTextBox
    Inherits System.Windows.Forms.TextBox

    ' Color used for background and foreground when
    ' control is diabled
    Private _BackColorDisabled As Color = System.Drawing.SystemColors.Control
    Private _ForeColorDisabled As Color = Color.Black
    Private _DisabledFont As Font = New System.Drawing.Font("Times New Roman", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))

    '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, _DisabledFont, _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
            '**this only works if the font is the default font**
            Me.Font = _DisabledFont
            '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