Custom WaterMark TextBox for Windows Forms - VB.Net

Published on
23,290 Points
3 Endorsements
Last Modified:
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.  


Form with textboxes and watermarks

1. Create VB class

Create a new VB class in your solution and name it WaterMarkTextBox.vb

2. Copy VB code to new class

Copy the code from the snippet below to your new class
Option Strict On
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Public Class WaterMarkTextBox
    Inherits TextBox
    Private oldFont As Font = Nothing
    Private waterMarkTextEnabled As Boolean = False
#Region "Attributes"
    Private _waterMarkColor As Color = Drawing.Color.Gray
    Public Property WaterMarkColor() As Color
            Return _waterMarkColor
        End Get
        Set(ByVal value As Color)
            _waterMarkColor = value
        End Set
    End Property
    Private _waterMarkText As String = "Water Mark"
    Public Property WaterMarkText() As String
            Return _waterMarkText
        End Get
        Set(ByVal value As String)
            _waterMarkText = value
        End Set
    End Property
#End Region
    ' Default constructor
    Public Sub New()
    End Sub
    Private Sub JoinEvents(ByVal join As Boolean)
        If join Then
            AddHandler (TextChanged), AddressOf WaterMark_Toggle
            AddHandler (LostFocus), AddressOf WaterMark_Toggle
            AddHandler (FontChanged), AddressOf WaterMark_FontChanged
            'No one of the above events will start immeddiatlly 
            'TextBox control still in constructing, so,
            'Font object (for example) couldn't be catched from within WaterMark_Toggle
            'So, call WaterMark_Toggel through OnCreateControl after TextBox is totally created
            'No doupt, it will be only one time call
            'Old solution uses Timer.Tick event to check Create property
        End If
    End Sub
    Private Sub WaterMark_Toggle(ByVal sender As Object, ByVal args As EventArgs)
        If Me.Text.Length <= 0 Then
        End If
    End Sub
    Private Sub WaterMark_FontChanged(ByVal sender As Object, ByVal args As EventArgs)
        If waterMarkTextEnabled Then
            oldFont = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)
        End If
    End Sub
    Private Sub EnableWaterMark()
        'Save current font until returning the UserPaint style to false (NOTE: It is a try and error advice)
        oldFont = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)
        'Enable OnPaint Event Handler
        Me.SetStyle(ControlStyles.UserPaint, True)
        Me.waterMarkTextEnabled = True
        'Trigger OnPaint immediatly
    End Sub
    Private Sub DisableWaterMark()
        'Disbale OnPaint event handler
        Me.waterMarkTextEnabled = False
        Me.SetStyle(ControlStyles.UserPaint, False)
        'Return oldFont if existed
        If Not oldFont Is Nothing Then
            Me.Font = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)
        End If
    End Sub
    ' Override OnCreateControl 
    Protected Overrides Sub OnCreateControl()
        WaterMark_Toggle(Nothing, Nothing)
    End Sub
    ' Override OnPaint
    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        ' Use the same font that was defined in base class
        Dim drawFont As Font = New Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit)
        ' Create new brush with gray color or 
        Dim drawBrush As SolidBrush = New SolidBrush(Me.WaterMarkColor) 'use WaterMarkColor
        ' Draw Test or WaterMark
        e.Graphics.DrawString(IIf(waterMarkTextEnabled, WaterMarkText, Text).ToString(), drawFont, drawBrush, New Point(0, 0))
    End Sub
End Class

Open in new window

3. Compile application to use new WaterMarkTextBox control

Compile the application.  You should now see a "<Your Project Name> Components" section on your toolbox with a tool called WaterMarkTextBox

4. Use the new WaterMarkTextBox control

Drag the new control from your toolbox onto your form and you can set the properties in the properties window (WaterMarkColor, WaterMarkText)


Author Comment

Thank you to the original author of the C# code that accomplished this same task.  Wael Alghool

Expert Comment

thank you for this. looks and works great!

Expert Comment

Nice!  I tried other solutions that all had drawbacks.  This works really well.  Thanks!
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 11

Expert Comment

by:Deepak Lakkad
Thanks for this article. It works well.

I have a quetion regarding this.

I have changed the fonts of form, but when i create new WaterMarkTextBox it remains in default font.

How can i change it to take default fonts of the form?

- Deepak Lakkad

Expert Comment

Dear Rhino 1272
how do you obtain the shadow effect in your textbox as in your IMAGE OF WATERMARKS

Expert Comment

by:ram prasad chakraborty
It really works well.

Expert Comment

by:Dewang Desai
Great Stuff.

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Join & Write a Comment

Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month