Default Text for a TextBox in Windows Phone 7 apps

In this post I will implement a Default Text property for a Windows Phone 7 TextBox control. This property could be useful in such applications like Twitter, Rss Reader clients etc. Take a look at webcast bellow.

To make it possible to specify a Default Text value for a Windows Phone TextBox control we need to create a new class, inherit a TextBox and add one property and override some events. New property will hold a value for a default text. You also need to override GotFocus and LostFocus events to manipulate with text property. Here is my code for creating default text feature:

public class EDAdvancedTextbox: TextBox
{
    private string _defaultText = string.Empty;
    public string DefaultText
    {
        get
        {
            return _defaultText;
        }
        set
        {
            _defaultText = value;
            SetDefaultText();
        }
    }

    public EDAdvancedTextbox()
    {
        this.GotFocus += (sender, e) =>
            { if (this.Text.Equals(DefaultText)) { this.Text = string.Empty; } };
        this.LostFocus += (sender, e) => { SetDefaultText(); };
    }

    private void SetDefaultText()
    {
        if (this.Text.Trim().Length == 0)
            this.Text = DefaultText;
    }
}

To specify this custom control in my XAML I have used the following line:

<local:EDAdvancedTextbox x:Name="tbSearch" DefaultText="enter username..." />



This entry was posted in Development, Toolkit, Windows Phone 7 and tagged , , . Bookmark the permalink.

7 Responses to Default Text for a TextBox in Windows Phone 7 apps

  1. Pingback: Android Phone Q&A:How to permanently uninstall text message app on android phone? (Motorola backflip)? | What Is The Best Android Phone

  2. Pupugnao says:

    Nice Blog! Thanks for this article

  3. Cipres says:

    took off some work from me, thank you

  4. dimitbeba says:

    set
    {
    _defaultText = value;
    SetDefaultText();

    what meaning?

  5. h3nk3 says:

    You need to change some lines in order to use localization bindings:

    public static readonly DependencyProperty DefaultTextProperty = DependencyProperty.Register("DefaultText", typeof(string), typeof(EAdvancedTextbox), new PropertyMetadata(""));

    public string DefaultText
    {
    get
    {
    return (string)GetValue(DefaultTextProperty);
    }
    set
    {
    SetValue(DefaultTextProperty, value);
    SetDefaultText();
    }
    }

    and also add a handler for the loaded event last in the constructor:

    this.Loaded += (sender, e) => { SetDefaultText(); };

    Without these changes you get an exception when trying to bind values to DefaultText in your xaml files.

    Thanks for the article!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Comments links could be nofollow free.

Notify me of followup comments via e-mail. You can also subscribe without commenting.