title: Clear RichTextBox Before Writing New Text
body: <p>I'm using Visual Studio, WPF, C#, XAML.</p>
  
  <p>I have a RichTextBox that C# writes text to on button press.</p>
  
  <p>Each time the button is pressed it <em>appends</em> the new text to the RichTextBox, leaving the old text.</p>
  
  <p>I need it to clear the old text before writing the new, so it doesn't stack up.</p>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>&lt;RichTextBox Name="richTextBox1" IsUndoEnabled="False" /&gt;
  </code></pre>
  
  <p><strong>C#</strong></p>
  
  <p>Clear</p>
  
  <pre><code>richTextBox1.Document.Blocks.Clear();
  </code></pre>
  
  <hr>
  
  <pre><code>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
  }
  </code></pre>