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.
IMAGE OF WATERMARKS
IMAGE OF WATERMARKS
STEPS
- 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 Get Return _waterMarkColor End Get Set(ByVal value As Color) _waterMarkColor = value Me.Invalidate() End Set End Property Private _waterMarkText As String = "Water Mark" Public Property WaterMarkText() As String Get Return _waterMarkText End Get Set(ByVal value As String) _waterMarkText = value Me.Invalidate() End Set End Property #End Region ' Default constructor Public Sub New() JoinEvents(True) 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 EnableWaterMark() Else DisableWaterMark() 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) Refresh() 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 Refresh() 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() MyBase.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)) MyBase.OnPaint(e) End Sub
End Class
For C#:
using Microsoft.VisualBasic; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.Windows.Forms; using System.Drawing; public class WaterMarkTextBox : TextBox { private Font oldFont = null; private bool waterMarkTextEnabled = false; #region "Attributes" private Color _waterMarkColor = System.Drawing.Color.Gray; public Color WaterMarkColor { get { return _waterMarkColor; } set { _waterMarkColor = value; this.Invalidate(); } } private string _waterMarkText = "Water Mark"; public string WaterMarkText { get { return _waterMarkText; } set { _waterMarkText = value; this.Invalidate(); } } #endregion // Default constructor public WaterMarkTextBox() { JoinEvents(true); } private void JoinEvents(bool @join) { if (@join) { (TextChanged) += WaterMark_Toggle; (LostFocus) += WaterMark_Toggle; (FontChanged) += 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 } } private void WaterMark_Toggle(object sender, EventArgs args) { if (this.Text.Length <= 0) { EnableWaterMark(); } else { DisableWaterMark(); } } private void WaterMark_FontChanged(object sender, EventArgs args) { if (waterMarkTextEnabled) { oldFont = new Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit); Refresh(); } } private void 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 this.SetStyle(ControlStyles.UserPaint, true); this.waterMarkTextEnabled = true; //Trigger OnPaint immediatly Refresh(); } private void DisableWaterMark() { //Disbale OnPaint event handler this.waterMarkTextEnabled = false; this.SetStyle(ControlStyles.UserPaint, false); //Return oldFont if existed if ((oldFont != null)) { this.Font = new Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit); } } // Override OnCreateControl protected override void OnCreateControl() { base.OnCreateControl(); WaterMark_Toggle(null, null); } // Override OnPaint protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { // Use the same font that was defined in base class Font drawFont = new Font(Font.FontFamily, Font.Size, Font.Style, Font.Unit); // Create new brush with gray color or SolidBrush drawBrush = new SolidBrush(this.WaterMarkColor); //use WaterMarkColor // Draw Test or WaterMark e.Graphics.DrawString((waterMarkTextEnabled ? WaterMarkText : Text).ToString(), drawFont, drawBrush, new Point(0, 0)); base.OnPaint(e); }
}
- 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)
No comments:
Post a Comment