Search code examples
c#richtextboxvisual-studio-express

Clear Text in RichTextBox in C#


I have a RichTextBox, and I am wanting to code it, that when the user left clicks in the box, that the text in that text box, clears out.

Please can someone help me out?

My code I have tried is:

private void richTextBox1_TextChanged(object sender, EventArgs e)
{
    richTextBox1.text = "";
}

At the moment I have the box with "Input Text Here" written in it (under the Text in the Properties Section) - so when the user clicks inside the box, it will clear that text,so the user can type text in there.

Thank you.


Solution

  • Try this

    private void richTextBox1_Click(object sender, EventArgs e)
    {
      if (richTextBox1.Text == "Input Text Here")
      {
        richTextBox1.Clear();
        richTextBox1.Focus();
      }
    }
    

    It checks wether default text is there or not, if it is it clears it and gives the richbox focus so you can enter text. Otherwise, it procedes with the regular textchanging.