Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 137,858 times

Contents

Related Categories

RichTextBox Control - Tab Stops

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

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: Read Only Property

    Posted by mmarksbury on 24 Apr 2008

    This is long past the topic post date but...

     I used the selection method (where you select text and then update it), for formatting the output of an application's process in a RTF. ...

  • Re: Read Only Property

    Posted by magiel on 06 Jan 2007

    I am had the same proplems;


    RichTextBox1.SelectionFont.Bold
    RichTextBox1.SelectionFont.Underline etc are readonly


    Solved them the following way


    ...

  • Re: Pre-formatted text/Cursor position

    Posted by Verdi on 21 Sep 2006

    Kylua, your efforts were not wasted...


    You have probably saved me a week's worth of head-pounding. Why it is so difficult to programmatically modify RTF using this control is beyond me. All...

  • Re: Pre-formatted text/Cursor position

    Posted by Kylua on 31 Jul 2006

    I spent AGES working thru this one and couldn't find it anywhere!


    The basic problem is that you have to update the richtextbox.rtf, not .text.


    And it is very fussy about goes in the...

  • can't get EM_SETTEXTMODE to respond

    Posted by vb_lover on 06 Dec 2005

    James Crowley:

    I am using vb6sp6(EM_SETTEXTMODE didn't work with vb6sp4 either) RTB control, I have already implemented URL detection similar to yours. However now i am trying to get MULTI-LEVEL U...