Search code examples
c#stringwindows-phone-8textboxkeyup

How to Empty TextBox Text in an event windows phone 8?


I want to Empty The Text which is by default written in the textbox , i can empty it by doing

Textbox1.Text="";

but i want to know, which event should i exactly use ?will it be GotFocus,KeyEnter,Keyup,Tap or any other method .


Solution

  • One simple way is using on GotFocus and LostFocus. On GotFocus the code should be something like this:

    if (textbox1.Text == "My default text...")
                textbox1.Text = "";
    

    Just to make sure to no delete a text previously entered by the user. And on LostFocus just something like this:

    if (String.IsNullOrWhiteSpace(textbox1.Text))
                textbox1.Text = "My default text...";
    

    So again you are checking to no delete a user entered text.

    Hope this helps