ComponentOne RichTextBox for WPF
Working with WPF RichTextBox / C1RichTextBox Concepts and Main Properties
In This Topic
    C1RichTextBox Concepts and Main Properties
    In This Topic

     On the surface, the C1RichTextBox control appears just like a standard TextBox. It provides the same properties to control the font, colors, text, and selection. That can be an advantage – if you have an application that uses TextBox controls, you may be able to simply replace them with C1RichTextBox controls without any additional changes.

    For example, the following code implements a simple search-and-replace routine that works on TextBox and on C1RichTextBox controls:

     

    Visual Basic
    Copy Code
    Private Sub SearchAndReplace(tb As TextBox, find As String, replace As String)
        Dim start As Integer = 0
        While True
            Dim pos As Integer = tb.Text.IndexOf(find, start)
            If pos < 0 Then
                Exit While
            End If
            tb.[Select](pos, find.Length)
            ' Optionally show a dialog box to confirm the change.
            tb.SelectedText = replace
            start = pos + 1
        End While
    End Sub
    
    C#
    Copy Code
    void SearchAndReplace(TextBox tb, string find, string replace)
    {
      for (int start = 0; ; )
      {
        int pos = tb.Text.IndexOf(find, start);
        if (pos < 0) break;
        tb.Select(pos, find.Length);
        // Optionally show a dialog box to confirm the change.
        tb.SelectedText = replace;
        start = pos + 1;
      }
    }
    

    The code looks for matches in the C1RichTextBox.Text property. It selects each match using the C1RichTextBox.Select method, and then replaces the text using the SelectedText property. To convert this method for use with the C1RichTextBox control, you would simply change the type of the first argument to use a C1RichTextBox instead of a regular TextBox.

    This is what the C1RichTextBox has in common with the regular TextBox. But of course it goes way beyond that. Suppose you wanted to highlight the replacements with a yellow background. This would be impossible with a regular TextBox. With the C1RichTextBox, you could accomplish that with one additional line of code:

    Visual Basic
    Copy Code
    Private Sub SearchAndReplace(tb As TextBox, find As String, replace As String)
        Dim start As Integer = 0
        While True
            Dim pos As Integer = tb.Text.IndexOf(find, start)
            If pos < 0 Then
                Exit While
            End If
            tb.[Select](pos, find.Length)
            ' Optionally show a dialog box to confirm the change.
            tb.Selection.InlineBackground = New SolidColorBrush(Colors.Yellow)
            tb.SelectedText = replace
            start = pos + 1
        End While
    End Sub
    
    C#
    Copy Code
    void SearchAndReplace(TextBox tb, string find, string replace)
    {
      for (int start = 0; ; )
      {
        int pos = tb.Text.IndexOf(find, start);
        if (pos < 0) break;
        tb.Select(pos, find.Length);
        // Optionally show a dialog box to confirm the change.
        tb.Selection.InlineBackground = new SolidColorBrush(Colors.Yellow);
        tb.SelectedText = replace;
        start = pos + 1;
      }
    }
    

    The C1RichTextBox.Selection property provides properties that allow you to inspect and modify the formatting of the current selection. With this property and the ones in common with the TextBox control, you can easily create documents and add rich formatting.

    You could use the technique described above to implement a toolbar or to add syntax coloring to documents. These topics are described in more detail in later sections.

    For example, the following code implements a simple search-and-replace routine that works on TextBox and on C1RichTextBox controls:
    C#
    Copy Code
    // Searching TextBox
    private void SearchAndReplace(TextBox tb, string find, string replace)
    {
        for (int start = 0; ;)
        {
            int pos = tb.Text.IndexOf(find, start);
            if (pos < 0) break;
            tb.Select(pos, find.Length);
            // Optionally show a dialog box to confirm the change.
            tb.SelectedText = replace;
            start = pos + 1;
        }
    }
    

    The code looks for matches in the C1RichTextBox.Text property. It selects each match using the C1RichTextBox.Select method, and then replaces the text using the SelectedText property. To convert this method for use with the C1RichTextBox control, you would simply change the type of the first argument to use a C1RichTextBox instead of a regular TextBox.

    This is what the C1RichTextBox has in common with the regular TextBox. But of course it goes way beyond that. Suppose you wanted to highlight the replacements with a yellow background. This would be impossible with a regular TextBox. With the C1RichTextBox, you could accomplish that with one additional line of code:

    C#
    Copy Code
    // Searching C1RichTextbox
    private void SearchAndReplace(C1RichTextBox tb, string find, string replace)
    {
        for (int start = 0; ;)
        {
            int pos = tb.Text.IndexOf(find, start);
            if (pos < 0) break;
            tb.Select(pos, find.Length);
            // Optionally show a dialog box to confirm the change.
    
            // changing replaced text color;
            tb.Selection.InlineBackground = Brushes.Yellow;
            tb.SelectedText = replace;
            start = pos + 1;
        }
    }
    

    The C1RichTextBox.Selection property provides properties that allow you to inspect and modify the formatting of the current selection. With this property and the ones in common with the TextBox control, you can easily create documents and add rich formatting.

    You could use the technique described above to implement a toolbar or to add syntax coloring to documents. These topics are described in more detail in later sections.