Watermark / hint text / placeholder TextBox

后端 未结 30 3080
遇见更好的自我
遇见更好的自我 2020-11-22 02:20

How can I put some text into a TextBox which is removed automatically when user types something in it?

30条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 02:43

    Set up the text box with placeholder text in a soft color...

    public MainWindow ( )
    {
        InitializeComponent ( );
        txtInput.Text = "Type something here...";
        txtInput.Foreground = Brushes.DimGray;
    }
    

    When the text box gets the focus, clear it and change the text color

    private void txtInput_GotFocus ( object sender, EventArgs e )
    {
        MessageBox.Show ( "got focus" );
        txtInput.Text = "";
        txtInput.Foreground = Brushes.Red;
    }
    

提交回复
热议问题