Silverlight-textbox with watermark

Source: Internet
Author: User

The watermark function of textbox is provided in silverlight2. However, this function is deleted in later versions. For more information about the watermark function in silverlight2, see this article.ArticleLearn Silverlight 2 series (2): basic controls step by step. Then I want to write one watermark by myself.

Below is a textbox with a watermark written by myself.

1. Create a class mytextbox that inherits the textbox. 2. Add a watermarktext attribute to the mytextbox class to save the watermark.

In addition to adding an attribute, you also need to add some global variables that save the attributes different from the normal state.

//Watermark statusPrivateBrush _ redcolor =NewSolidcolorbrush (colors. Red );Private Double_ Halfopacity =0.5;//NormalPrivateBrush _ usercolor;Private Double_ Useropacity;Public StringWatermarktext {Get;Set;}

 

3. Rewrite the ongotfocus () and onlostfocus () events.

In Textbox, we can find that these two events are marked by override, so we can reload them.

 Protected   Override   Void  Ongotfocus (routedeventargs e ){  If ( This . Text = Watermarktext ){  This . Text = ""  ;  This . Foreground = _ Usercolor; This . Opacity = _ Useropacity ;}  Base  . Ongotfocus (E );}  Protected   Override   Void  Onlostfocus (routedeventargs e ){  If ( This . Text. Length < 1  ){  This . Text = Watermarktext; This . Foreground = _ Redcolor;  This . Opacity = _ Halfopacity ;}  Base  . Onlostfocus (E );} 

 

4. Although most of the work has been completed here, there is another important point.

Similar to initialization, the system checks whether a watermark exists and sets a watermark. I willCodeWritten in the sizechanged event. For more information about the control lifecycle, see the Silverlight control lifecycle-Neil Chen. In addition, we need to initialize _ usercolor and _ useropacity.

The code for the sizechanged event is as follows:

 Public  Mytextbox () {sizechanged + =New  Sizechangedeventhandler (mytextbox_sizechanged );}  Void Mytextbox_sizechanged ( Object  Sender, sizechangedeventargs e) {_ usercolor = This  . Foreground; _ useropacity = This  . Opacity;  If (Watermarktext! = ""  ){  This . Foreground =_ Redcolor;  This . Opacity = _ Halfopacity;  This . Text = Watermarktext ;}} 

 

5. Source code , So far the work has been completed. The complete code is as follows: Textbox with watermark

 Using  System;  Using  System. net;  Using  System. windows; Using  System. Windows. controls;  Using  System. Windows. documents;  Using  System. Windows. Ink;  Using  System. Windows. input;  Using  System. Windows. Media;  Using  System. Windows. Media. animation;  Using  System. Windows. shapes;  Namespace Textboxwatermark {  Public   Class  Mytextbox: textbox {  //  Watermark status          Private Brush _ redcolor = New  Solidcolorbrush (colors. Red );  Private   Double _ Halfopacity = 0.5  ;  //  Normal         Private  Brush _ usercolor;  Private   Double  _ Useropacity;  Public   String Watermarktext { Get ; Set  ;}  Public  Mytextbox () {sizechanged + = New  Sizechangedeventhandler (mytextbox_sizechanged );} Void Mytextbox_sizechanged ( Object  Sender, sizechangedeventargs e) {_ usercolor = This  . Foreground; _ useropacity = This  . Opacity;  If (Watermarktext! = ""  ){  This . Foreground = _ Redcolor; This . Opacity = _ Halfopacity;  This . Text = Watermarktext ;}}  Protected   Override   Void  Ongotfocus (routedeventargs e ){  If ( This . Text = Watermarktext ){  This . Text = "" ;  This . Foreground = _ Usercolor;  This . Opacity = _ Useropacity ;}  Base  . Ongotfocus (E );}  Protected   Override   Void  Onlostfocus (routedeventargs e ){  If ( This . Text. Length <1  ){  This . Text = Watermarktext;  This . Foreground = _ Redcolor;  This . Opacity = _ Halfopacity ;}  Base  . Onlostfocus (e );}}} 

 

6. Call Process
<Local: mytextboxForeground= "Blue"Watermarktext= "Please input! " />

 

Local is the namespace where the mytextbox class is located. The local code is written as follows: xmlns: Local = "CLR-namespace: textboxwatermark"

 

As follows:

Focus not obtained:

Get focus and enter

 

Poor memory

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.