RichTextBox in Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This lesson describes the RichTextBox control


RichTextBox

This control is a text box that supports rich text extensions, which let the control display text that is bold, underlined, italicized, indented, in different fonts, and has other special visual properties.

The RichTextBox can load and save its contents in plain text (.txt) files (in which case the formatting is lost) or in Rich Text Format (.rtf) files (in which case the formatting is preserved).

You can use the RichTextBox control's Select method to select some of its text, and also you can other control's properties to change the appearance of the selected text.

    RichTextBox1.Select(0, 15)


select

For example, the following code sets the selection's color to black and makes its font bold.

    RichTextBox1.SelectionColor = Color.Black
    RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)


You can improve the previous example, adding more control over the text appearance e.g.

    Private Sub BoldButton_Click(sender As Object, e As EventArgs) _
     Handles BoldButton.Click
        RichTextBox1.SelectionColor = Color.Black
        RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
    End Sub

    Private Sub ItalicButton_Click(sender As Object, e As EventArgs) _
     Handles ItalicButton.Click
        RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Italic)
    End Sub

    Private Sub UnderlineButton_Click(sender As Object, e As EventArgs) _
     Handles UnderlineButton.Click
        RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Underline)
    End Sub

    Private Sub HeaderButton_Click(sender As Object, e As EventArgs) _
     Handles HeaderButton.Click
        RichTextBox1.SelectionFont = New Font(RichTextBox1.Font.Name, 15, FontStyle.Bold)
    End Sub


example

You can use the CanUndo and CanRedo properties to determine when you should enable Undo and Redo buttons.

The following code demonstrates how to manage Undo and Redo buttons for the RichTextBox control.

When the control's contents change, the TextChanged event handler enables or disables the buttons, while the buttons simply call the Undo and Redo methods respectively.

    Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) _
     Handles RichTextBox1.TextChanged
        UndoButton.Enabled = RichTextBox1.CanUndo
        RedoButton.Enabled = RichTextBox1.CanRedo
    End Sub

    Private Sub UndoButton_Click(sender As Object, e As EventArgs) _
     Handles UndoButton.Click
        If RichTextBox1.CanUndo Then
            RichTextBox1.Undo()
        End If
    End Sub

    Private Sub RedoButton_Click(sender As Object, e As EventArgs) _
     Handles RedoButton.Click
        If RichTextBox1.CanRedo Then
            RichTextBox1.Redo()
        End If
    End Sub


undo and redo

Previous Lesson | Next Lesson


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.