In this article will explain with an example, how to use RichTextBox in Windows Forms (WinForms) Application using C# and VB.Net.
 
 
Form Design
The following Form consists of:
RichTextBox – For writing and displaying content.
Button – For applying different format (style) to the RichTextBox content.
Using RichTextBox in Windows Forms using C# and VB.Net
 
 
Applying Bold to Selected Text
When Bold button is clicked, the font of the current text selection is referenced using SelectionFont property of RichtextBox.
Then, the new Font instance is created from the referenced selected font and Bold style is applied to it using Bold property of FontStyle class.
Finally, the font is applied to the selected text using SelectionFont property of the RichTextBox.
C#
private void OnBold(object sender, EventArgs e)
{
    Font selectedTextFont = richTextBox1.SelectionFont;
    if (selectedTextFont != null)
    {
        richTextBox1.SelectionFont = new Font(selectedTextFont, selectedTextFont.Style ^ FontStyle.Bold);
    }
}
 
VB.Net
Private Sub OnBold(sender As Object, e As EventArgs) Handles btnBold.Click
    Dim selectedTextFont As Font = richTextBox1.SelectionFont
    If selectedTextFont IsNot Nothing Then
        richTextBox1.SelectionFont = New Font(selectedTextFont, selectedTextFont.Style Xor FontStyle.Bold)
    End If
End Sub
 
 
Applying Italic to Selected Text
When Italic button is clicked, the font of the current text selection is referenced using SelectionFont property of RichtextBox.
Then, the new Font instance is created from the referenced selected font and Italic style is applied to it using Italic property of FontStyle class..
Finally, the font is applied to the selected text using SelectionFont property of the RichTextBox.
C#
private void OnItalic(object sender, EventArgs e)
{
    Font selectedTextFont = richTextBox1.SelectionFont;
    if (selectedTextFont != null)
    {
        richTextBox1.SelectionFont = new Font(selectedTextFont, selectedTextFont.Style ^ FontStyle.Italic);
    }
}
 
VB.Net
Private Sub OnItalic(sender As Object, e As EventArgs) Handles btnItalic.Click
    Dim selectedTextFont As Font = richTextBox1.SelectionFont
    If selectedTextFont IsNot Nothing Then
        richTextBox1.SelectionFont = New Font(selectedTextFont, selectedTextFont.Style Xor FontStyle.Italic)
    End If
End Sub
 
 
Applying Underline to Selected Text
When Underline button is clicked, the font of the current text selection is referenced using SelectionFont property of RichTextBox.
Then, the new Font instance is created from the referenced selected font and Underline style is applied to it using Underline property of FontStyle class.
Finally, the font is applied to the selected text using SelectionFont property of the RichTextBox.
C#
private void OnUnderline(object sender, EventArgs e)
{
    Font selectedTextFont = richTextBox1.SelectionFont;
    if (selectedTextFont != null)
    {
        richTextBox1.SelectionFont = new Font(selectedTextFont, selectedTextFont.Style ^ FontStyle.Underline);
    }
}
 
VB.Net
Private Sub OnUnderline(sender As Object, e As EventArgs) Handles btnUnderline.Click
    Dim selectedTextFont As Font = richTextBox1.SelectionFont
    If selectedTextFont IsNot Nothing Then
        richTextBox1.SelectionFont = New Font(selectedTextFont, selectedTextFont.Style Xor FontStyle.Underline)
    End If
End Sub
 
 
Applying Color to Selected Text
When Color (A) button is clicked, ColorDialog object is created and ColorDialog is shown.
When the color is selected from the ColorDialog, the font of the current text selection is referenced using SelectionFont property.
Finally, the color is applied to the selected text using SelectionColor property of the RichTextBox.
C#
private void OnColorChange(object sender, EventArgs e)
{
    ColorDialog colorDialog = new ColorDialog();
    if (colorDialog.ShowDialog() == DialogResult.OK)
    {
        Font selectedTextFont = richTextBox1.SelectionFont;
        if (selectedTextFont != null)
        {
            richTextBox1.SelectionColor = colorDialog.Color;
        }
    }
}
 
VB.Net
Private Sub OnColorChange(sender As Object, e As EventArgs) Handles btnSelectColor.Click
    Dim colorDialog As ColorDialog = New ColorDialog()
    If colorDialog.ShowDialog() = DialogResult.OK Then
        Dim selectedTextFont As Font = richTextBox1.SelectionFont
        If selectedTextFont IsNot Nothing Then
            richTextBox1.SelectionColor = colorDialog.Color
        End If
    End If
End Sub
 
 
Displaying RichTextBox Content
When Submit Button is clicked, the contents of the RichTextBox1 are displayed in RichTextBox2.
C#
private void OnSubmit(object sender, EventArgs e)
{
    richTextBox2.Rtf = richTextBox1.Rtf;
}    
 
VB.Net
Private Sub OnSubmit(sender As Object, e As EventArgs) Handles btnSubmit.Click
    richTextBox2.Rtf = richTextBox1.Rtf
End Sub
 
 
Screenshot
Using RichTextBox in Windows Forms using C# and VB.Net
 
 
Downloads