Working With RichTextBox in WPF


There is no such method to directly assign a string value, get the contents and clear the contents of a RichTextBox. This article shows how to do that.
Set Content

Using the TextRange class in the System.Windows.Documents namespace a string value can't be assigned to a RichTextBox as the contents. See the following code.
  1. Stream SM = new MemoryStream(Encoding.UTF8.GetBytes(stringValue));  
  2. TextRange range;  
  3. range = new TextRange(RichTextBox.Document.ContentStart, RichTextBox.Document.ContentEnd);  
  4. range.Load(SM, System.Windows.DataFormats.Text);  
  5. SM.Close();  
In the code line 1, the string value is converted into a stream using the MemoryStream class in the System.IO namespace. This conversion is done because the Load function of TextRange class does accept a stream as parameter. This load function helps set the content of a RichTextBox. Code line 2 creates an object of the TextRange class and line 3 initializes the TextRange class. The constructor of the TextRange class takes the start and end text pointer of the RichTextBox. In the preceding ContentStart is the start text pointer and ContentEnd is the end text pointer. Code line 4 calls the Load function of the TextRange class that accepts two parameters, a System.IO.Stream and a System.Windows.DataFormats.Text.
Getting Content 
We will get the content of the RichTextBox the same as the preceding code using the TextRange class.  See the following code.
  1. TextRange range;    
  2. range = new TextRange(RichTextBox.Document.ContentStart, RichTextBox.Document.ContentEnd);   
  3. string Richtextvalue=textRange.Text;  
In the preceding code, an object of the TextRange class is created and a ContentStart and ContentEnd are passed to the constuctor. The ContentStart and ContentEnd indicate the range of the contents to extract. The ContentStart and ContentEnd properties each return a TextPointer and are accessible on the underlying FlowDocument that represents the contents of the RichTextBox. TextRange provides a Text property that returns the plain text portions of the TextRange as a string.
Remove Content 
  1. RichTextBox.Document.Blocks.Clear();  
The preceding code helps to remove all the contents from the RichTextBlock. The RichTextBlock treats the contents as a block. Hence removing the block means removing the contents of the RichTextBlock.
Selecting All Content

The SelectAll function of RichTextBlock selects all the text of a RichTextBlock .
  1. RichTextBlock.SelectAll();  
Getting Selected Text

The selection property  helps to retrieve the selected text from the RichTextBox. The data type of selection is the TextSelection class in the System.Windows.Documents namespace. It's a sealed class.
  1. TextSelection TS = RichTextBlock.Selection;  
  2. string selectedText = TS.Text;  
Selecting Some Text

The follwoing code selects some text in the RichTextBox.
  1. // Create two arbitrary TextPointers to specify the range of content to select.  
  2.            TextPointer myTextPointer1 = RichTextBlock.Document.ContentStart.GetPositionAtOffset(20);  
  3.            TextPointer myTextPointer2 = RichTextBlock.Document.ContentStart.GetPositionAtOffset(20);  
  5.            // Programmatically change the selection in the RichTextBox.  
  6.            RichTextBlock.Selection.Select(myTextPointer1, myTextPointer2);  

I hope you have learned from the article a little about RichTextBox.