ComponentOne RichTextBox for WPF
Working with the C1Document Object / Using the C1Document Class
In This Topic
    Using the C1Document Class
    In This Topic

    As we mentioned earlier, the C1RichTextBox provides a linear, flat view of the control content, while C1Document class exposes the document structure.

    To illustrate the advantages of working directly with the document object, suppose you wanted to add some functionality to the previous sample: when the user presses the CTRL key, you want to capitalize the text in all paragraphs of type Heading2.

    In the following section learn how use DocumentClass in RichTextBox .NET 4.5.2 and .NET 5 versions.

    The object model exposed by the C1RichTextBox is not powerful enough to do this reliably. You would have to locate spans based on their formatting, which would be inefficient and unreliable (what if the user formatted some plain text with the same format used by Heading2?).

    Using the C1Document object model, this task becomes trivial:

    Visual Basic
    Copy Code
    Public Sub New()
        ' Default initialization
        InitializeComponent()
        ' No changes here...
        ' Bind the second C1RichTextBox to the same document
        rtb2.Document = _rtb.Document
        AddHandler rtb2.KeyDown, AddressOf rtb2_KeyDown
    End Sub
    Private Sub rtb2_KeyDown(sender As Object, e As KeyEventArgs)
        If e.Key = Key.Ctrl Then
            For Each heading2 As var In _rtb.Document.Blocks.OfType(Of Heading2)()
                Dim text = heading2.ContentRange.Text
                heading2.ContentRange.Text = text.ToUpper()
            Next
        End If
    End Sub
    
    C#
    Copy Code
    public MainPage()
    {
      // Default initialization
      InitializeComponent();
      // No changes here...
      // Bind the second C1RichTextBox to the same document
      rtb2.Document = _rtb.Document;
      rtb2.KeyDown += rtb2_KeyDown;
    }
    void rtb2_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.Key == Key.Ctrl)
      {
        foreach (var heading2 in _rtb.Document.Blocks.OfType<Heading2>())
        {
          var text = heading2.ContentRange.Text;
          heading2.ContentRange.Text = text.ToUpper();
        }
      }
    }
    

    The code monitors the keyboard. When the user presses the CTRL key, it enumerates all Heading2 elements in the document and replaces their content with capitals.

    Doing the same thing using the C1RichTextBox object model would require a lot more code and the result would not be as reliable.

    The object model exposed by the C1RichTextBox is not powerful enough to do this reliably. You would have to locate spans based on their formatting, which would be inefficient and unreliable (what if the user formatted some plain text with the same format used by Heading2?).

    Using the C1Document object model, this task becomes trivial:

    C#
    Copy Code
    public C1DocumentClass()
    {
        InitializeComponent();
    
        richTextBox.Document = DocumentCreator.DocumentAssembly(typeof(C1RichTextBox).Assembly);
        richTextBox.IsReadOnly = true;
    
        // Bind the second C1RichTextBox to the same document
        richTextBox2.Document = richTextBox.Document;
    
        richTextBox2.KeyDown += RichTextBox2KeyDown;
    }
    
    private void RichTextBox2KeyDown(object sender, KeyEventArgs e)
    {
        // For converting each heading2 to upper case when ctrl key is pressed.
        if (e.Key == Key.LeftCtrl)
        {
            foreach (var heading2 in richTextBox.Document.Blocks.OfType<Heading2>())
            {
                var text = heading2.ContentRange.Text;
                heading2.ContentRange.Text = text.ToUpper();
            }
        }
    }
    

    The code monitors the keyboard. When the user presses the CTRL key, it enumerates all Heading2 elements in the document and replaces their content with capitals.

    Doing the same thing using the C1RichTextBox object model would require a lot more code and the result would not be as reliable.