RichTextBox Overview
Collapse the table of content
Expand the table of content

RichTextBox Overview


The RichTextBox control enables you to display or edit flow content including paragraphs, images, tables, and more. This topic introduces the TextBox class and provides examples of how to use it in both Extensible Application Markup Language (XAML) and C#.

Both RichTextBox and TextBox allow users to edit text, however, the two controls are used in different scenarios. A RichTextBox is a better choice when it is necessary for the user to edit formatted text, images, tables, or other rich content. For example, editing a document, article, or blog that requires formatting, images, etc is best accomplished using a RichTextBox. A TextBox requires less system resources then a RichTextBox and it is ideal when only plain text needs to be edited (i.e. usage in forms). See TextBox Overview for more information on TextBox. The table below summarizes the main features of TextBox and RichTextBox.


Real-time Spellchecking

Context Menu

Formatting commands like ToggleBold (Ctr+B)

FlowDocument content like images, paragraphs, tables, etc.











Note: Although TextBox does not support formatting related commands like ToggleBold (Ctr+B), many basic commands are supported by both controls such as MoveToLineEnd.

The features from the table above are covered in more detail later.

The code below shows how to create a RichTextBox that a user can edit rich content in.

Specifically, the content edited in a RichTextBox is flow content. Flow content can contain many types of elements including formatted text, images, lists, and tables. See Flow Document Overview for in depth information on flow documents. In order to contain flow content, a RichTextBox hosts a FlowDocument object which in turn contains the editable content. To demonstrate flow content in a RichTextBox, the following code shows how to create a RichTextBox with a paragraph and some bolded text.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Documents;
namespace SDKSample
    public partial class BasicRichTextBoxWithContentExample : Page
        public BasicRichTextBoxWithContentExample()
            StackPanel myStackPanel = new StackPanel();

            // Create a FlowDocument to contain content for the RichTextBox.
            FlowDocument myFlowDoc = new FlowDocument();

            // Create a Run of plain text and some bold text.
            Run myRun = new Run("This is flow content and you can ");
            Bold myBold = new Bold(new Run("edit me!"));

            // Create a paragraph and add the Run and Bold to it.
            Paragraph myParagraph = new Paragraph();

            // Add the paragraph to the FlowDocument.

            RichTextBox myRichTextBox = new RichTextBox();

            // Add initial content to the RichTextBox.
            myRichTextBox.Document = myFlowDoc;

            this.Content = myStackPanel;


The following illustration shows how this sample renders.

RichTextBox with content

Elements like Paragraph and Bold determine how the content inside a RichTextBox appears. As a user edits RichTextBox content, they change this flow content. To learn more about the features of flow content and how to work with it, see Flow Document Overview.

Note: Flow content inside a RichTextBox does not behave exactly like flow content contained in other controls. For example, there are no columns in a RichTextBox and hence no automatic resizing behavior. Also, built in features like search, viewing mode, page navigation, and zoom are not available within a RichTextBox.

You can enable real-time spell checking in a TextBox or RichTextBox. When spellchecking is turned on, a red line appears underneath any misspelled words (see picture below).

Textbox with spell-checking

See How to: Enable Spell Checking in a Text Editing Control to learn how to enable spellchecking.

By default, both TextBox and RichTextBox have a context menu that appears when a user right-clicks inside the control. The context menu allows the user to cut, copy, or paste (see illustration below).

TextBox with context menu

You can create your own custom context menu to override the default one. See How to: Position a Custom Context Menu in a RichTextBox for more information.

Editing commands enable users to format editable content inside a RichTextBox. Besides basic editing commands, RichTextBox includes formatting commands that TextBox does not support. For example, when editing in a RichTextBox, a user could press Ctr+B to toggle bold text formatting. See EditingCommands for a complete list of commands available. In addition to using keyboard shortcuts, you can hook commands up to other controls like buttons. The following example shows how to create a simple tool bar containing buttons that the user can use to change text formatting.

The following illustration shows how this sample displays.

RichTextBox with ToolBar

Usually the TextChanged event should be used to detect whenever the text in a TextBox or RichTextBox changes rather then KeyDown as you might expect. See How to: Detect When Text in a TextBox Has Changed for an example.

The following example shows how to save content of a RichTextBox to a file, load that content back into the RichTextBox, and print the contents. Below is the markup for the example.

Below is the code behind for the example.

© 2016 Microsoft