Make a Watermark TextBox in Silverlight in few
steps-
When we make a textbox like to search the
content or any other we want to show some text at the start and when user click
on that textbox then the text will empty.
Suppose u make a form that contain two textbox
for first name and last name. When form load for the user simplicity we have
want to show which textbox is for what , so the watermark textbox can help for
this.
To make the watermark textbox do the following steps.
- Create new silverlight project name as
TextBoxWatermark
- In the xaml page add one textbox name as
textbox1.
- Set height and width of the textbox.
- In the TextBoxWatermark.xaml.cs file go to
the constructor and add two events for it name as LostFocus and GotFocus. As
like following
private
void txtSearch_LostFocus(object
sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
{
textBox1.Text = "Search ";
}
}
private void
txtSearch_GotFocus(object sender,
RoutedEventArgs e)
{
textBox1.Text
= string.Empty;
}
When lostfocus - Add the watermark text in the textbox using the .Text
property.
when gotgocus- make the watermark string empty.
- Run the project and see output.
- Done.