RichTextBox Control

Tab Stops

Tab Stops allow you to specify the intervals the cursor moves to when you press the tab key. (Try right clicking on the ruler in word to see an example). The Rich Text Box supports this, but you must note that in order for it to work, the tab values have to be in order. For this I use a list box. The current tabs are loaded into the list box, and can be removed or added and when the dialog is closed, the contents of the list box loaded back into the SelTabs property. Note that the property name is SelTabs, ie that they are not applied to the whole document.

Create a form called frmMain with a rich text box on it called txtText. Then create a form named frmTabStops, and add the following controls:

Control Type Name Properties
Combo Box cboTabs Style = Simple Combo; Sorted = False
Command Button cmdOK  
Command Button cmdAdd  
Command Button cmdRemove  

I use the following code:

' Form load code
Private Sub Form_Load()
    On Error Resume Next
    ' Add the tab stops
    For i = 0 To frmMain.txtText.SelTabCount - 1
        ' Add the item to the list box
        cboTabs.AddItem CInt((frmMain.txtText.SelTabs(i) * 100) / 100#)
    Next
    cboTabs.ListIndex = 0
End Sub

' Add tab code
Public Sub cmdAdd_Click()
    Dim gItemFound As Boolean
    gItemFound = False
    ' Add new value
    For i = 0 To cboTabs.ListCount - 1
        ' Is the current item more than
        ' new one?
        If Val(cboTabs.List(i)) > Val(cboTabs.Text) Then
            ' Yes it is, add the new
            ' item here
            cboTabs.AddItem cboTabs.Text, i
            ' select this item
            cboTabs.ListIndex = i
            ' we have put it in
            gItemFound = True
            Exit For
        End If
Next
If gItemFound = False Then
    ' No item found that is greater
    ' put the new item at bottom
    cboTabs.AddItem cboTabs.Text, cboTabs.ListCount
    ' select the item
    cboTabs.ListIndex = cboTabs.ListCount - 1
End If

End Sub

' Remove tab code
Private Sub cmdRemove_Click()
    ' Are you sure?
    If MsgBox("Remove current tab stop?", vbYesNo + vbExclamation) = vbNo Then Exit Sub
    cboTabs.RemoveItem (cboTabs.ListIndex)
End Sub

' OK code
Public Sub cmdOK_Click()
    ' Update sel tabs
    frmMain.txtText.SelTabCount = cboTabs.ListCount
    For i = 0 To cboTabs.ListCount - 1
        ' Add the sel tab
        frmMain.txtText.SelTabs(i) = cboTabs.List(i)
    Next
    Unload Me
End Sub

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programs must be written for people to read, and only incidentally for machines to execute.”