Troubles overriding textbox

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.

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.
 
Hi,

I have created this class for you to demonstrate how you can create a Textbox with a Watermark property and thereafter control what is displayed in the Textbox when the focus enters and leaves the control. Add this to a new class:-

VB.NET:
Public Class WatermarkTextBox
  Inherits TextBox
 
  Private _Watermark As String
  Private SavedBackColor As Color
  Private SavedForeColor As Color
  Private SavedFont As Font
 
  Public Property Watermark As String
    Get
      Return _Watermark
    End Get
    Set(value As String)
      _Watermark = value
 
      SavedBackColor = MyBase.BackColor
      SavedForeColor = MyBase.ForeColor
      SavedFont = MyBase.Font
 
      MyBase.Text = _Watermark
      MyBase.ForeColor = WaterMarkForeColor
      MyBase.BackColor = WaterMarkBackColor
      MyBase.Font = WaterMarkFont
    End Set
  End Property
 
  Public Property WaterMarkForeColor As Color = Color.LightGray
  Public Property WaterMarkBackColor As Color = Color.White
  Public Property WaterMarkFont As Font = New Font(MyBase.Font.FontFamily, MyBase.Font.Size, FontStyle.Italic)
 
  Public Sub myTextBoxEnter(sender As Object, e As System.EventArgs) Handles MyBase.Enter
    If Not Watermark = String.Empty AndAlso MyBase.Text = Watermark Then
      MyBase.Clear()
      MyBase.Font = SavedFont
      MyBase.ForeColor = SavedForeColor
      MyBase.BackColor = SavedBackColor
    End If
  End Sub
 
  Public Sub myTextBoxLeave(sender As Object, e As System.EventArgs) Handles MyBase.Leave
    If MyBase.Text = String.Empty Then
      If Not _Watermark = String.Empty Then
        Watermark = _Watermark
      End If
    End If
  End Sub
End Class
Add this code to the load event of a new form:-

VB.NET:
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim wmtxtEmailAddress As New WatermarkTextBox
    With wmtxtEmailAddress
      .Name = "wmtxtEmailAddress_1"
      .Width = 200
      .Watermark = "Please Enter an Email Address"
    End With
    Me.Controls.Add(wmtxtEmailAddress)
  End Sub
Run the application and see what you are presented with. Enter some text and leave the control to see what happens, then remove the text to leave the Textbox empty and then leave the control to see what happens.

Once you read the code in the class you should be able to get a good handle on what is happening and the watermark code may need a few tweaks to perform the way you want but this should give you a good start.

You may also find that this is better in a user control so that you can add the control to your forms and intertact with it's properties as you need.

Just thought, make sure the control does not have the focus on the form load, it will remove the watermark waiting for text to be entered. Add a few other contols first to make sure they have the focus on load.

Cheers,

Ian
 
Last edited:
Brilliant Ian! Thanks so much. You are absolutely the "Forum Genius". Not only did you answer my one question but you answered several others as well (ones I figured the answers would come in time).

Love the way you set up the properties. Seemed a bit awkward to have to do the get/set just to get something from the property control panel. Guessing this is not a VS2008 thing so I"m migrating up to VS2010 (was fighting that question in case someone was using W2K).

Yes. I will be including this in the toolbox. Is there a better way other than right clicking the toolbox and adding it by the "choose items..." feature (you mentioned putting it in a user control, I thought it was one, just inheriting from the textbox class)?

There are a few other things I'll be adding to it. Seems a fantastic way of removing some excess code from the main project.

Thanks again!
 
Hi,

You are welcome and silly as this may sound, I did not expect the class to be available in the Toolbox being defined as just a class. I have always been taught to create this as a User Control for it to be available in the toolbox. As it is, just click your Application Components menu in the toolbox and it will be available to you.

I have just checked my own components and it's there for me to add controls as I need. You learn something new every day.

Good luck and let us know if you need further help.

Cheers,

Ian
 
By all means. If creating it as a User Control is preferred I'd better learn how to do it. Now that I know the power of creating user defined controls I'll be doing more. Is there a direction you can point me in (or could you recreate the textbox as a user control to show me?).
 
Hi,

As you would expect, I am an advocate of helping people but more importantly helping people to help themselves. On this basis I cannot at this time create the user control for you since you need to learn from the experience of doing it for yourself.

If you search the web, you will find loads of examples of user control creation, but to get you started try this UTube example:-

How to create and use User Controls in VB.NET - YouTube

I have not watched this myself but it is a good 20 minutes long so it should give you a good idea of how to get started.

By all means post back if you are struggling with any concepts and we will always try and help you further.

Cheers,

Ian
 
Ian?

Regarding the creation of a 'user control'. Is there a way to get the tool tip to function?

I've created a control, on that little form they give you and it works fine, but when I add it and the tool tip control to a main form, the tool tip will not show?

Thanks much. When I get this more smoothed out I'll post my code.
 
Hi,

Just to be sure you have got everything populated, when you add a tooltip control to a form it then adds an additional property to each and every control on the form i.e. there should now be a new property on your user control called "ToolTip on ToolTip1". Add some descriptive text to the property for a tool tip to be shown.

If you have already done the above, then try reducing the InitialDelay and AutoPopDelay properties of the ToolTip control. It may well be that all is fine but you are just not seeing the tooltip.

Hope that helps.

Cheers,

Ian
 
Back
Top