在 C# 中清除 RichTextBox 中的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10281520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:09:50  来源:igfitidea点击:

Clear Text in RichTextBox in C#

c#richtextboxvisual-studio-express

提问by Kevdog777

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.

我有一个 RichTextBox,我想对它进行编码,当用户在框中左键单击时,该文本框中的文本会被清除。

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.

谢谢你。

采纳答案by Bruno Charters

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.

它检查默认文本是否存在,如果存在则清除它并提供丰富的焦点,以便您可以输入文本。否则,它将继续进行常规文本更改。

回答by bytecode77

What you're looking for is a Cue Textbox. You can do this with one WINAPI call.

您正在寻找的是一个提示文本框。您可以通过一次 WINAPI 调用来完成此操作。

See this as a reference: http://www.codeproject.com/Articles/18858/Fully-themed-Windows-Vista-Controls

将此作为参考:http: //www.codeproject.com/Articles/18858/Fully-themed-Windows-Vista-Controls

回答by Mitja Bonca

Subscribe to MouseClick of rtb, then do:

订阅 rtb 的 MouseClick,然后执行:

    private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            richTextBox1.Clear();
        }
    }

回答by ToyAuthor X

I am VS2015 user.

我是 VS2015 用户。

richTextBox1.Clear();                 // Form
richTextBox1.Document.Blocks.Clear(); // WPF