Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
RichTextBox Class
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
RichTextBox Class

Represents a rich editing control which operates on FlowDocument objects.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/xaml/presentation
Visual Basic (Declaration)
<LocalizabilityAttribute(LocalizationCategory.Inherit)> _
<ContentPropertyAttribute("Document")> _
Public Class RichTextBox _
    Inherits TextBoxBase _
    Implements IAddChild
Visual Basic (Usage)
Dim instance As RichTextBox
C#
[LocalizabilityAttribute(LocalizationCategory.Inherit)]
[ContentPropertyAttribute("Document")]
public class RichTextBox : TextBoxBase, 
    IAddChild
Visual C++
[LocalizabilityAttribute(LocalizationCategory::Inherit)]
[ContentPropertyAttribute(L"Document")]
public ref class RichTextBox : public TextBoxBase, 
    IAddChild
JScript
public class RichTextBox extends TextBoxBase implements IAddChild
XAML Object Element Usage
<RichTextBox>
  Document
</RichTextBox>

FlowDocument is the only supported child element for a RichTextBox.

ms612650.alert_caution(en-us,VS.90).gifImportant Note:

RichTextBox has built-in handling for the bubbling  MouseUp and MouseDown events. Consequently, custom event handlers that listen for MouseUp or MouseDown events from a RichTextBox will never be called. If you need to respond to these events, listen for the tunneling PreviewMouseUp and PreviewMouseDown events instead, or register the handlers with the HandledEventsToo argument (this latter option is only available through code). Do not mark the event handled unless you deliberately want to disable RichTextBox native handling of these events, and be aware that this has notable effects on the control's UI.

While RichTextBox supports copying and pasting of images, certain scenarios are currently unsupported and may not work properly. The following table summarizes these scenarios and expected results.

Image Copy Source

Image Paste Result

Image source is a graphic metafile

No image is pasted

Image source uses relative path or link

Garbled or blank image is pasted

Image source link does not end with an expected image format (.PNG, .JPG, .GIF)

Garbled or blank image is pasted

Image source copied from malformed RichText (RTF)

Link to image source is pasted (rather than image)

Pasting HTML content into a RichTextBox might result in unexpected behavior because RichTextBox uses RTF format rather then directly using HTML format.

Text always wraps in a RichTextBox. If you do not want text to wrap then set the PageWidth on the FlowDocument to be larger than the width of the RichTextBox. However, once the page width is reached the text still wraps.

Horizontally and vertically aligning content within a RichTextBox is done with the HorizontalContentAlignment and VerticalContentAlignment properties. Aligning the RichTextBox within the layout of the page is done with the HorizontalAlignment and VerticalAlignment properties.

Scrollbars are not visible on a RichTextBox by default. To make a scrollbar visible, set the VerticalScrollBarVisibility property to Visible or Auto.

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

RichTextBox supports a variety of keyboard commands. For a list of keyboard commands, see EditingCommands.

The following example declares a RichTextBox that contains a simple FlowDocument.

C#
<RichTextBox Name="richTB">
  <FlowDocument>
    <Paragraph>
      <Run>Paragraph 1</Run>
    </Paragraph>
    <Paragraph>
      <Run>Paragraph 2</Run>
    </Paragraph>
    <Paragraph>
      <Run>Paragraph 3</Run>
    </Paragraph>
  </FlowDocument>
</RichTextBox>

C#
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Documents;
namespace SDKSample
{
    public partial class RichTextBoxExample : Page
    {
        public RichTextBoxExample()
        {

            StackPanel myStackPanel = new StackPanel();

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

            // Add paragraphs to the FlowDocument.
            myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 1")));
            myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
            myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
            RichTextBox myRichTextBox = new RichTextBox();

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

            myStackPanel.Children.Add(myRichTextBox);
            this.Content = myStackPanel;

        }
    }
}

More Code

How to: Extract the Text Content from a RichTextBox This example shows how to extract the contents of a RichTextBox as plain text.
How to: Position a Custom Context Menu in a RichTextBox This example shows how to position a custom context menu for a RichTextBox.
How to: Replace the Default Content Host for a RichTextBox This example shows how to use Windows Presentation Foundation (WPF) styles to replace the default content host for a RichTextBox.
System..::.Object
  System.Windows.Threading..::.DispatcherObject
    System.Windows..::.DependencyObject
      System.Windows.Media..::.Visual
        System.Windows..::.UIElement
          System.Windows..::.FrameworkElement
            System.Windows.Controls..::.Control
              System.Windows.Controls.Primitives..::.TextBoxBase
                System.Windows.Controls..::.RichTextBox
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows Vista

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker