Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 456
Kevdog777 Avatar asked Oct 28 '25 17:10

Kevdog777


1 Answers

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.

like image 56
Bruno Charters Avatar answered Oct 31 '25 08:10

Bruno Charters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!