Kevdog777 Kevdog777 - 3 years ago 243
C# Question

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.

Answer Source

Try this

private void richTextBox1_Click(object sender, EventArgs e)
  if (richTextBox1.Text == "Input Text Here")

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download