Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleAdd numbered lists and special bullets to the RichTextBox control in Visual Basic .NET
DescriptionThis example shows how to add numbered lists and special bullets to the RichTextBox control in Visual Basic .NET.
KeywordsRTF, RichTextBox, Rich Text, VB.NET
CategoriesControls
 
Normally to format text in a RichTextBox control, you set its SelStart and SelLength properties to select the text you want to format. You then use the control's properties such as SelectedBullet and SelectedBold to format the text.

However, Rich Text provides a huge number of features not provided by the RichTextBox control. Two of these are numbered lists and bulleted lists with special bullets. This example adds RTF formatting codes to the control's text to provide those features.

If you select some text in the control and click the Numbered List button, the following code executes. This routine begins by defining string constants that represent pieces of the RTF formatting codes. It uses the RichTextBox control's SelectedText property to get the selected text and splits the text into lines.

The control then starts building the RTF codes it will add to the control. It starts with the codes in RTF_NUMSTART and the first line of text.

Then for each remaining line in the selected text, the code adds the line followed by the codes in RTF_NUMITEM.

After adding all of the lines, the code removes the final carriage return and line feed and adds the RTF codes in RTF_NUMEND.

Finally the code sets the RichTextBox control's SelectedRTF property to the RTF codes it has built.

 
Private Sub btnNumberedList_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnNumberedList.Click
    Const RTF_NUMSTART As String = _
        "{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS " & _
            "Sans Serif;}{\f1\froman\fcharset2 " & _
            "Symbol;}{\f2\fnil\fprq2\fcharset2 " & _
            "Wingdings;}{\f3\froman\fprq2 Times New " & _
            "Roman;}}" & vbCrLf & _
        "{\colortbl\red0\green0\blue0;}" & vbCrLf & _
        "\deflang1033\pard\li720\fi-360\plain\f3\fs24 " & _
            "1.\tab "
    Const RTF_NUMITEM As String = _
        "\par @%@.\tab "
    Const RTF_NUMEND As String = "\par }"

    ' Get the current text.
    Dim old_text As String = _
        rtfTest.SelectedText.Replace(vbCrLf, vbLf)
    Dim lines() As String = Split(old_text, vbLf)

    ' Start the list.
    Dim new_text As String = RTF_NUMSTART & lines(0) & _
        vbCrLf

    ' Add the other lines.
    For i As Integer = 1 To lines.Length - 1
        new_text &= _
            RTF_NUMITEM.Replace("@%@", (i + 1).ToString()) _
                & _
            lines(i) & vbCrLf
    Next i

    ' Remove the final vbCrLf.
    new_text = new_text.Substring(0, new_text.Length - _
        vbCrLf.Length)

    ' End the list.
    new_text &= RTF_NUMEND

    ' Save the result.
    rtfTest.SelectedRtf = new_text

    rtfTest.Focus()
End Sub
 
If you select some text in the control and click the Special Bullets button, the following code executes. This code is similar to the previous code except it uses different RTF codes for the list's start, items, and end.
 
Private Sub btnSpecialBullets_Click(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    btnSpecialBullets.Click
    Const RTF_BULSTART As String = _
        "{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS " & _
            "Sans Serif;}{\f1\froman\fcharset2 " & _
            "Symbol;}{\f2\froman\fprq2 Times New " & _
            "Roman;}{\f3\fnil\fprq2\fcharset2 Wingdings;}}" _
            & vbCrLf & _
        "{\colortbl\red0\green0\blue0;}" & vbCrLf & _
        "\deflang1033\pard\li1080\fi-360\tx1080\plain\f3\fs24 " & _
            "v\tab \plain\f2\fs24 "
    Const RTF_BULITEM As String = _
        "\par \plain\f3\fs24 v\tab \plain\f2\fs24 "
    Const RTF_BULEND As String = "\par }"

    ' Get the current text.
    Dim old_text As String = _
        rtfTest.SelectedText.Replace(vbCrLf, vbLf)
    Dim lines() As String = Split(old_text, vbLf)

    ' Start the list.
    Dim new_text As String = RTF_BULSTART & lines(0) & _
        vbCrLf

    ' Add the other lines.
    For i As Integer = 1 To lines.Length - 1
        new_text &= _
            RTF_BULITEM.Replace("@%@", (i + 1).ToString()) _
                & _
            lines(i) & vbCrLf
    Next i

    ' Remove the final vbCrLf.
    new_text = new_text.Substring(0, new_text.Length - _
        vbCrLf.Length)

    ' End the list.
    new_text &= RTF_BULEND

    ' Save the result.
    rtfTest.SelectedRtf = new_text

    rtfTest.Focus()
End Sub
 

Notes

  • When you select text, don't include the following carriage return. If you do, then that carriage return becomes part of the list and you get a blank item that is either numbered or bulletted.
  • To create other, similar effects, use WordPad to create an RTF file that holds the effects. Then open the file in Notepad to learn the RTF codes that you need.
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated