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 IfEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: