Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB5,VB6
Expertise: Intermediate
Jun 16, 2001

Let the user add tab characters to a RichTextBox control

If a form contains a RichTextBox control and other controls that are capable to receive the input focus, when the focus is on the RichTextBox and the user presses the Tab key to add a space, the focus will be moved to the following control in the tab order, and the tab character won't be added. The solution is check the KeyCode value of theKeyDown events: if it is vbKeyTab, you can programmatically add a tab space and set the KeyCode parameter to 0, to avoid the focus to be moved. Here's an example:

Private Sub RichTextBox1_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyTab Then
        ' add the tab space
        RichTextBox1.SelText = vbTab
        ' set KeyCode to 0 to avoid the focus to be moved
        KeyCode = 0
    End If
End Sub
Marco Bellinaso
Thanks for your registration, follow us on our social networks to keep up-to-date