In this article I will explain how you can Watermark textbox
in Windows Application Using c#.net and Vb.Net. This article will explain you How
to add watermark property in Textbox of WinForm or in a desktop application using C#.net. Shadow text in a windows application using C#.Net and VB.Net.
Some of my previous articles are as follows: Bind
DataGridView In Windows Application Using C#, Pass
Value From One Form to Another Form in Windows Application C#.Net,VB.Net, How
To Make a Single Row of DataGridview Bold Using C#.Net in Windows Application,
Bind
DataGridView In Windows Application Using C#, How
to Minimize an Application to the Taskbar Tray in C#.Net | System Tray
Application C#, Email
Validation in Windows Application C#.Net and VB.Net | Validating Email ID in
TextBox in C# .Net.
C#.Net
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DemoWindowsApplication
{
public partial class WaterMark : Form
{
public
WaterMark()
{
InitializeComponent();
textBox1.ForeColor = Color.LightGray;
textBox1.Text = "Please Enter Name";
this.textBox1.Leave
+= new System.EventHandler(this.textBox1_Leave);
this.textBox1.Enter
+= new System.EventHandler(this.textBox1_Enter);
textBox2.ForeColor = Color.LightGray;
textBox2.Text = "Please Enter Address";
this.textBox2.Leave
+= new System.EventHandler(this.textBox2_Leave);
this.textBox2.Enter
+= new System.EventHandler(this.textBox2_Enter);
}
private
void textBox1_Leave(object
sender, EventArgs e)
{
if
(textBox1.Text == "")
{
textBox1.Text = "Please Enter Name";
textBox1.ForeColor = Color.Gray;
}
}
private
void textBox1_Enter(object
sender, EventArgs e)
{
if
(textBox1.Text == "Please Enter Name")
{
textBox1.Text = "";
textBox1.ForeColor = Color.Black;
}
}
private
void textBox2_Enter(object
sender, EventArgs e)
{
if
(textBox2.Text == "Please Enter
Address")
{
textBox2.Text = "";
textBox2.ForeColor = Color.Black;
}
}
private
void textBox2_Leave(object
sender, EventArgs e)
{
if
(textBox2.Text == "")
{
textBox2.Text = "Please Enter Address";
textBox2.ForeColor = Color.Gray;
}
}
}
}
|
VB.Net
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Namespace DemoWindowsApplication
Partial Public Class WaterMark
Inherits
Form
Public
Sub New()
InitializeComponent()
textBox1.ForeColor =
Color.LightGray
textBox1.Text = "Please Enter Name"
Me.textBox1.Leave
+= New System.EventHandler(Me.textBox1_Leave)
Me.textBox1.Enter
+= New System.EventHandler(Me.textBox1_Enter)
textBox2.ForeColor = Color.LightGray
textBox2.Text = "Please Enter Address"
Me.textBox2.Leave
+= New System.EventHandler(Me.textBox2_Leave)
Me.textBox2.Enter
+= New System.EventHandler(Me.textBox2_Enter)
End Sub
Private
Sub textBox1_Leave(ByVal
sender As Object,
ByVal e As
EventArgs)
If
textBox1.Text = "" Then
textBox1.Text = "Please Enter Name"
textBox1.ForeColor =
Color.Gray
End
If
End Sub
Private
Sub textBox1_Enter(ByVal
sender As Object,
ByVal e As
EventArgs)
If
textBox1.Text = "Please Enter Name"
Then
textBox1.Text = ""
textBox1.ForeColor =
Color.Black
End
If
End Sub
Private
Sub textBox2_Enter(ByVal
sender As Object,
ByVal e As
EventArgs)
If
textBox2.Text = "Please Enter Address"
Then
textBox2.Text = ""
textBox2.ForeColor =
Color.Black
End
If
End Sub
Private
Sub textBox2_Leave(ByVal
sender As Object,
ByVal e As
EventArgs)
If
textBox2.Text = "" Then
textBox2.Text = "Please Enter Address"
textBox2.ForeColor =
Color.Gray
End
If
End Sub
End Class
End Namespace
|
In above code I have fired event in when mouse cursor leave
the textbox and enter in the textbox, we are assigning the value and making the
color of textbox text change. Which provide watermark effect.
Now run the application
Is it possible to have both the grey text and cursor at the same time?
ReplyDeleteYes we can. Just apply color attribute to textbox as gray
Delete