Introduction
In this blog I have created a water mark TextBox using base control, I took custom control project and did some 
modification on the base control.
To make a water mark user control follow the following steps given bellow
- Create new console control project
 - name the project name as you want to name 
the controls.
 - Add a class and rename that as 
per-requirement
 - Inherit that class from TextBoxclass
 
In this article I have named the control as 
MyTextBox, declare a public string property as NullText and set the value to 
'Null Text'.
Code
public
string _NullText = 
"Null Text";
public 
stringNullText {
get
{
return 
_NullText;
}
set
{
_NullText = value;
}
}
In the initialized event set the font color to gray and text to NullText if the 
textbox contains only nulltext, if not contains set the font color to black.
 
Code
 
if 
(string.IsNullOrEmpty(this.Text))
{
this.Text 
= NullText;
this.Foreground 
= Brushes.LightGray;
}
In the gotfucuse event clear the text of text box if it contains text same as 
nulltext and set the fore color to gray, if not fore color will be black. See 
the following code written bellow:
Code
 
if 
(this.Text == NullText)
{
this.Clear();
this.Foreground 
= Brushes.LightGray;
}
else
this.Foreground 
= Brushes.Black; 
 
In the lost focuse event if the text box text is equals to nulltext then set the 
fore color to gray and if the text is equal to empty thatn set the text to 
nulltext other wise set the color to black
 
See the following code :
Code
 
private
void MyTextBox_LostFocus(object 
sender, RoutedEventArgs e)
{
this.Text 
= (this.Text == string.Empty 
? NullText : this.Text);
if 
(this.Text != NullText)
{
this.Foreground 
= Brushes.Black;
}
else
this.Foreground 
= Brushes.LightGray; 
}
 
Full Code
 
public
class MyTextBox 
: TextBox
{
public
string _NullText = 
"Null Text";
public 
stringNullText {
get
{
return 
_NullText;
}
set
{
_NullText = value;
}
}
public 
MyTextBox()
{
this.GotFocus 
+= new RoutedEventHandler(MyTextBox_GotFocus);
this.LostFocus 
+= new RoutedEventHandler(MyTextBox_LostFocus);
this.TextChanged 
+= new 
TextChangedEventHandler(MyTextBox_TextChanged);
this.Initialized 
+= new EventHandler(MyTextBox_Initialized);
this.KeyDown+=new 
KeyEventHandler(MyTextBox_KeyDown);
}
 
void 
MyTextBox_Initialized(object sender, EventArgs 
e)
{
if 
(string.IsNullOrEmpty(this.Text))
{
this.Text 
= NullText;
this.Foreground 
= Brushes.LightGray;
}
}
void 
MyTextBox_KeyDown(object sender, KeyEventArgs e)
{
if 
(string.IsNullOrEmpty(this.Text))
{
this.Text 
= NullText;
}
e.Handled =
false;
}
void 
MyTextBox_TextChanged(object sender, 
TextChangedEventArgs e)
{
if 
(this.Text != NullText)
{
this.Foreground 
= Brushes.Black;
} 
}
private
void MyTextBox_GotFocus(object 
sender, RoutedEventArgs e)
{ 
if 
(this.Text == NullText)
{
this.Clear();
this.Foreground 
= Brushes.LightGray;
}
else
this.Foreground 
= Brushes.Black; 
}
private
void MyTextBox_LostFocus(object 
sender, RoutedEventArgs e)
{
this.Text 
= (this.Text == string.Empty 
? NullText : this.Text);
if 
(this.Text != NullText)
{
this.Foreground 
= Brushes.Black;
}
else
this.Foreground 
= Brushes.LightGray; 
}
}