Remove text after clicking in the textbox

后端 未结 8 1008
孤独总比滥情好
孤独总比滥情好 2021-02-02 18:03

When you activate an application, a textbox with text \"hello\" will appear.

My question is:
When you click on the textbox in order to make input data, I want to rem

8条回答
  •  故里飘歌
    2021-02-02 18:34

    To expound upon Donut's answer so that your textbox will keep the text a user inputs unless it's purely whitespace, here's a solution that I use:

    XAML

    
    

    C#

    void TextBox_GotFocus( object sender, RoutedEventArgs e )
    {
        TextBox box = sender as TextBox;
        box.Text = string.Empty;
        box.Foreground = Brushes.Black;
        box.GotFocus -= TextBox_GotFocus;
    }
    
    void TextBox.LostFocus( object sender, RoutedEventArgs e )
    {
        TextBox box = sender as TextBox;
        if( box.Text.Trim().Equals( string.Empty ) )
        {
            box.Text = "Search...";
            box.Foreground = Brushes.LightGray;
            box.GotFocus += TextBox_GotFocus;
        }
    }
    

提交回复
热议问题