C# 如何清除 RichTextBox 中的文本内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10805134/
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 15:19:55  来源:igfitidea点击:

How to clear text content in RichTextBox

c#wpf

提问by Yasser

After getting the text in the RichTextBox I want to clear the text. How can I do that?

在 RichTextBox 中获取文本后,我想清除文本。我怎样才能做到这一点?

TextRange txt = new TextRange(richtxtSNotice.Document.ContentStart, richtxtSNotice.Document.ContentEnd);
MessageBox.Show(txt.Text);

采纳答案by Yasser

Try to create a TextRangewith RichBoxTextcontent, then set Textto empty string:

尝试创建一个TextRange包含RichBoxText内容,然后设置Text为空字符串:

TextRange txt = new TextRange(richtxtSNotice.Document.ContentStart, richtxtSNotice.Document.ContentEnd);
txt.Text = "";

回答by McAfeeJ

easiest way I know how is to put

我知道如何放置的最简单方法

       (your richTextbox name).text = "";

that tells it to replace anything in the textbox field with blank code. I'm sure there are other ways to do it too though.

这告诉它用空白代码替换文本框字段中的任何内容。我相信还有其他方法可以做到这一点。

回答by Curtis

Use the following:

使用以下内容:

_richTextBox.Document.Blocks.Clear();

回答by Adiel Yaacov

This is a simple way of doing it.

这是一个简单的方法。

        public void Clear()
        {
            richTextBox1.SelectAll();

            richTextBox1.Selection.Text = "";
        }

回答by GDye

I found that clearing the richTextBox didn't always remove all of the text from the richTextBox using richTextBox.Text = ""; or richTextBox.Clear(); Only the first few lines were cleared.

我发现清除richTextBox 并不总是使用richTextBox.Text = ""; 从richTextBox 中删除所有文本。或richTextBox.Clear(); 只有前几行被清除。

To fix this issue, I included the Update() function call solved this issue.

为了解决这个问题,我包含了 Update() 函数调用解决了这个问题。

richTextBox.Clear();

followed by

其次是

richTextBox.Update();

to reliably clear the richTextBox.

可靠地清除richTextBox。

回答by Abdel Mun'Im Odeh

To clear all content of richtext box you can use the following code

要清除富文本框的所有内容,您可以使用以下代码

richTextBox1->SelectAll();
richTextBox1->Clear();