ravenswood1000
Member
- Joined
- Aug 21, 2010
- Messages
- 8
- Programming Experience
- 10+
Hi everyone,
I'm working on creating some custom textboxes with properties that would be more tailored to what the project needs. I'm trying to avoid using labels for textboxes by placing "watermarks" inside. I've started working on textboxes with a "watermark" property which is really text that gets replaced. The trouble is the displayed properties don't get read till after the constructor is ran (which I expect). What I don't know is how to read the properties and do something with them after the constructor is complete.
I've included the code I'm working with in hopes someone can assist me.
Thanks for any help you folks can give me. This seems to be a very good way of avoiding code in the project itself.
I'm working on creating some custom textboxes with properties that would be more tailored to what the project needs. I'm trying to avoid using labels for textboxes by placing "watermarks" inside. I've started working on textboxes with a "watermark" property which is really text that gets replaced. The trouble is the displayed properties don't get read till after the constructor is ran (which I expect). What I don't know is how to read the properties and do something with them after the constructor is complete.
I've included the code I'm working with in hopes someone can assist me.
VB.NET:
Imports System.ComponentModel
Public Class BetterTextBox
Inherits TextBox
Private mWatermark As String = ""
Private mWatermarkForeColor As Color = Color.LightGray
Private mNormalForecolor As Color = Color.Black
Private mNormalBackcolor As Color = Color.White
Private mErrorForecolor As Color = Color.White
Private mErrorBackcolor As Color = Color.Salmon
Sub New()
mNormalForecolor = Me.ForeColor
mNormalBackcolor = Me.BackColor
MyBase.Text = Watermark
Me.Refresh()
End Sub
#Region "Override events and properties on inherited textbox"
Protected Overrides Sub OnGotFocus(ByVal e As System.EventArgs)
MyBase.OnGotFocus(e)
If Me.Text = mWatermark Then
Me.Text = String.Empty
End If
End Sub
Protected Overrides Sub OnLostFocus(ByVal e As System.EventArgs)
MyBase.OnLostFocus(e)
If Me.Text = String.Empty Then
Me.Text = mWatermark
End If
End Sub
#End Region
#Region "Subroutines"
Public Sub SetError()
Me.ForeColor = mErrorForecolor
Me.BackColor = mErrorBackcolor
End Sub
Public Sub SetNormal()
Me.ForeColor = mNormalForecolor
Me.BackColor = mNormalBackcolor
End Sub
#End Region
#Region "Properties"
Public Property ErrorForecolor() As Color
Get
Return mErrorForecolor
End Get
Set(ByVal value As Color)
mErrorForecolor = value
End Set
End Property
Public Property ErrorBackcolor() As Color
Get
Return mErrorBackcolor
End Get
Set(ByVal value As Color)
mErrorBackcolor = value
End Set
End Property
Public Property Watermark() As String
Get
Return mWatermark
End Get
Set(ByVal value As String)
mWatermark = value
End Set
End Property
#End Region
End Class
Thanks for any help you folks can give me. This seems to be a very good way of avoiding code in the project itself.