在编写新文本之前清除RichTextBox

时间:2017-06-23 17:33:15

标签: c# wpf visual-studio xaml

我使用的是Visual Studio,WPF,C#,XAML。

我有一个RichTextBox,C#在按下按钮时将文本写入。

每次按下按钮,新文本追加到RichTextBox,留下旧文本。

在编写新文本之前我需要它来清除旧文本,所以它不会叠加。

XAML

<RichTextBox Name="richTextBox1" IsUndoEnabled="False" />

C#

清除

richTextBox1.Document.Blocks.Clear();
public static System.Windows.Media.Brush White = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FFFFFF"));
public static Paragraph paragraph = new Paragraph();

// Button
private void btnWrite_Click(object sender, RoutedEventArgs e) {

    //Clear() put here, does nothing


    // New Document
    richTextBox1.Document = new FlowDocument(paragraph);


    //Clear() put here works but wont write new text, stays blank


    //begin writing
    richTextBox1.BeginChange();

        //Clear() put here works but wont write new text, stays blank

        //write text
        paragraph.Inlines.Add(new Run("Hello, World.") { Foreground = White });

    //end writing
    richTextBox1.EndChange();


    richTextBox1.UpdateLayout(); //does nothing
}

3 个答案:

答案 0 :(得分:2)

有三种方法可以清除RichTextBox的文本:

  1. 使用clear方法:

    richTextBox1.Document.Blocks.Clear();
    

    正如我所提到的,这种方式对你不起作用

  2. 选择所有RichTextBox并将文本设置为&#34;&#34;:

    richTextBox1.SelectAll();
    richTextBox1.Selection.Text = "";
    

    在开始更改之前,请执行此操作。

  3. 使用TextRange获取文本并使用&#34;&#34;清除它:

    TextRange txt = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
    txt.Text="";
    
  4. 我的回答是这个问题的结论:how to clear text content in RichTextBox

    更新:您可以尝试以下操作:

    1. 从FlowDocument创建一个对象:

      FlowDocument ObjFdoc = new FlowDocument();
      
    2. 创建一个段落:

      Paragraph paragraph = new Paragraph();
      paragraph.Inlines.Add(new Run("Hello, World.") { Foreground = White });
      
    3. 将段落添加到FlowDocument:

      ObjFdoc.Blocks.Add(ObjPara1);
      
    4. 设置RichtTextBox.Document:

      richTextBox1.Document=ObjFdoc;
      
    5. 每次调用方法时,都会生成一个新的flowdocument,gc会为你删除旧的flowdocuments。

答案 1 :(得分:1)

我需要50点评论......

每次按下按钮时,您是否有特殊原因要为FlowDocument分配新的RichTextBox

您正在使用Clear()方法清除文档内容,而不是将整个段落的新FlowDocument对象附加到RichTextBox,从而将新文本附加到同一段落

尝试删除richTextBox1.Document = new FlowDocument(paragraph);行。

答案 2 :(得分:0)

因为该文本已添加到FlowDocument( richtextBox1.Document),必须使用以下代码清除它:

 paragraph.Inlines.Clear(); 

说明: 这里的问题是,您正在清除FlowDocument中的所有块,包括单个paragraph,因此,下次您想在该段落中添加文本时,它将不起作用,当然,该文本会添加到所引用的段落中paragraph撰写,但已不再是文档的一部分。

这是一些单元测试

 [Apartment(ApartmentState.STA)]
    public class Tests
    {
        public static System.Windows.Media.Brush White = (SolidColorBrush)(new BrushConverter().ConvertFrom("#FFFFFF"));
        protected Paragraph paragraph;
        protected RichTextBox richTextBox1;

        [SetUp]
        public void Setup()
        {
            richTextBox1 = new RichTextBox();
            paragraph = new Paragraph();
            richTextBox1.Document = new FlowDocument(paragraph);
        }

        /// <summary>
        /// This reproduce the issue
        /// </summary>
        [Test]
        public void Adding_Text_After_Blocks_Clear_Should_Not_Work_The_issue()
        {
            paragraph.AddText("Hello, World!");

            string content1 = richTextBox1.GetText();

            Assert.AreEqual("Hello, World!", content1);

            richTextBox1.Document.Blocks.Clear();

            string content2 = richTextBox1.GetText();

            Assert.AreEqual("", content2);

            paragraph.AddText("Howdy!");

            string content3 = richTextBox1.GetText();

            //are not equal
            Assert.AreNotEqual("Howdy!", content3);
        }

        [Test]
        public void Adding_TextAfter_Paragraph_Clear_Should_Work_Solution()
        {
            paragraph.AddText("Hello, World!");

            string content1 = richTextBox1.GetText();

            Assert.AreEqual("Hello, World!", content1);

            paragraph.Inlines.Clear();

            string content2 = richTextBox1.GetText();

            Assert.AreEqual("", content2);

            paragraph.AddText("Howdy!");

            string content3 = richTextBox1.GetText();

            Assert.AreEqual("Howdy!", content3);
        }

        /// <summary>
        /// Keep in mind tha if the  RichTextBox is not readonly, deleting text with CTRL-A + DELETE 
        /// Causes the same issues.
        /// </summary>
        [Test]
        public void Clearing_Blocks_Should_Remove_All_Paragraphs_The_Cause_of_issue()
        {
            paragraph.AddText("Hello, World!");

            string content1 = richTextBox1.GetText();

            Assert.AreEqual("Hello, World!", content1);

            var blocksBefore = richTextBox1.Document.Blocks.ToArray();

            Assert.IsTrue(ReferenceEquals(paragraph, blocksBefore[0]));

            richTextBox1.Document.Blocks.Clear();

            var blocksAfter = richTextBox1.Document.Blocks.ToArray();

            Assert.AreEqual(0, blocksAfter.Length);
        }


        [Test]
        public void Added_New_Text_after_Blocks_Clear_Should_Work()
        {
            paragraph.AddText("Hello, World!");

            string content1 = richTextBox1.GetText();

            Assert.AreEqual("Hello, World!", content1);

            richTextBox1.Document.Blocks.Clear();

            string content2 = richTextBox1.GetText();

            Assert.AreEqual("", content2);

            var p = new Paragraph();

            p.AddText("Howdy!");

            //Add this to the blocks collection!!!!
            richTextBox1.Document.Blocks.Add(p);

            string content3 = richTextBox1.GetText();

            Assert.AreEqual("Howdy!", content3);
        }
    }

    public static class RTBExtentions
    {
        public static string GetText(this RichTextBox rtb)
        {
            return new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text.TrimEnd();
        }
        public static void AddText(this Paragraph paragraph, string textToAdd, Brush foregroundColor = default(Brush))
        {
            paragraph.Inlines.Add(new Run(textToAdd) { Foreground = foregroundColor });
        }

        public static Block[] ToArray(this BlockCollection blocks)
        {
            List<Block> elements = new List<Block>();

            foreach (var item in blocks)  {  elements.Add(item);  }

            return elements.ToArray();
        }
    }