简体   繁体   English

在 C# 中清除 RichTextBox 中的文本

[英]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.我有一个 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.谢谢你。

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.否则,它将继续进行常规文本更改。

Subscribe to MouseClick of rtb, then do:订阅 rtb 的 MouseClick,然后执行:

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

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

I am VS2015 user.我是 VS2015 用户。

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

This will work:这将起作用:

Go to the "properties" of your richtextbox.转到富文本框的“属性”。 You will see a yellow lightning bolt in the first line tab.您将在第一行选项卡中看到一个黄色闪电。 There you will find all possible events that can be triggered.在那里您会找到所有可能触发的事件。 Search for "Enter" or "Click" entry, double-click it.搜索“Enter”或“Click”条目,双击它。 There you can put whatever you want (such as richTextBox1.Text = "";)在那里你可以放任何你想要的东西(比如richTextBox1.Text = "";)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM