So far, we've only looked at the read-only wrappers for the FlowDocument, but WPF also includes a control which makes a FlowDocument editable: The RichTextBox control. You can add a RichTextBox directly to the window, without any content - in that case, it will automatically create a FlowDocument instance that you will be editing.
The RichTextBox can be a very useful control to take in text input from a user. With minimal work, you can create a reasonable text editor based on this control. But there’s not much point in having a multifunctional RichTextBox around if you can’t save the input or redisplay it when needed.
So in the next few blogs I’m going to demonstrate some of the ways you can load and save content with a WPF RichTextBox.Loading Plain Text FilesOK, so you’re probably thinking “But it’s a Rich Text Box! Why would I want to load a plain txt file?”. Well, one scenario is where you have a txt file and you want to enhance its formatting. You can load it in, edit it and make some changes to the text format, before saving it as an rtf file with its improved look (more on this in later blogs).Here’s the WPF Window I’m using:The XAML to create this is. RichTextBox On the btnLoad click, we need to get the text file, read all its content into a String and then paste this String into the RichTextBox. ‘Paste’ isn’t perhaps completely accurate; what actually happens is that the default Document inside the RichTextBox is comprised of FlowDocument Block elements.
The code below adds a new Block that contains text to the RTB’s Document. The content of that Block is a Paragraph containing the contents of the txt file. The easiest way to visualize it is as a cascading stack of content holders – RTB, Document, Block, Paragraph, Run.Here’s the code.
Dim DemoTextFile As String = 'C:Demo.txt'Private Sub btnLoadClick( ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)Dim newContent As String = IO.File.OpenText(DemoTextFile).ReadToEndRichTextBox1.Document.Blocks.Add( New Paragraph( New Run(newContent)))End SubSaving to a.txt FileSaving the document in the RichTextBox back to either the original text file or a new one requires that you create a TextRange object that will identify the start and end points of the content. In this example, in order to gather up all of the text, this TextRange will span from the start of the Document to the end. You then save this content range by using a FileStream that’s connected to the text file.
Additionally, you pass in a parameter that identifies the specific member of the DataFormats class - in this case, the plain Text format.Here’s the code for this task. Private Sub btnSaveClick( ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)Dim fs As IO.FileStream = IO.File.OpenWrite(DemoTextFile)Dim RTBText As New TextRange(RichTextBox1.Document.ContentStart, RichTextBox1.Document.ContentEnd)RTBText.Save(fs, DataFormats.Text)fs.Closefs.DisposeEnd SubClearing the RichTextBoxIf you try the above code to load from a text file, you’ll find that it appends the file contents to whatever already exists in the RichTextBox. Of course, you can select and delete anything you don’t want displayed or saved, but it would be a bit more professional to give the user a third button to clear the content.The following code gives you that facility.