Drydo's Blog

Teenager of the Internet

vbCity Blogs moved to:
http://cs.vbcity.com/blogs
  Home :: Syndication  :: Login   Community Forums   :: vbCity.com   :: DevCity.NET  

How's yer donald. Just noticed a newsgroup posting about some guy struggling with setting a custom forecolor on a textbox when its disabled (e.g. enabled = false). "No bother" I thought and quickly ran up the code - then I realised the dates, over two months ago and he had found a solution (although not as elegant).

Anyway, for your enjoyment and perusal. This class inherits a textbox, simply shadows the enabled property and if the property is set to false, it draws the textbox (with appropriate colors) through the onPaint event as opposed to being controlled by the form, which it does when the enabled property is set to true.

 

Code Copy HideScrollFull
Public Class VisualTextbox
    Inherits TextBox

    Public Sub New()
        ' Initialise the class
        MyBase.New()
    End Sub

    Public Shadows Property Enabled() As Boolean
        Get
            Return MyBase.Enabled
        End Get
        Set(ByVal Value As Boolean)
            ' Switch draw styles if disabled
            Me.SetStyle(ControlStyles.UserPaint, Not Value)
            ' Set the underlying value
            MyBase.Enabled = Value
        End Set
    End Property

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        MyBase.OnPaint(e)

        ' Draw the bg in
        e.Graphics.FillRectangle(New SolidBrush(Color.LightGray), Me.ClientRectangle)

        ' Draw the appropriate text in using the fore color
        e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Me.ForeColor), -1, 1)
    End Sub

End
Class
. . .
posted on Tuesday, June 07, 2005 3:24 PM