You are currently offline, waiting for your internet to reconnect

How To Insert TAB Characters in RichTextBox Control in VB

This article was previously published under Q143273
SUMMARY
The RichTextBox control allows you to create RTF documents from within yourVisual Basic program. If you press the TAB key while a RichTextBox controlhas the focus, the focus is moved to the next control in the tab orderrather than inserting a tab character into the RichTextBox control. Thisarticle explains how you can insert the TAB character into the RichTextBoxcontrol itself.
MORE INFORMATION
At run-time, a user must press CTRL+TAB to insert a TAB character in aRichTextBox control. However, most people are accustomed to just simplypressing the TAB key. Whenever the TAB key is pressed from within aRichTextBox control, the focus is immediately set to the next control onthe form.

In the demonstration program below, we change the TabStop property of allcontrols on the form to False. While the RichTextBox control has the focus,the TAB key is prevented from setting the focus to another control. This isbecause the TabStop property is set to False. Therefore, the TAB controlcharacter is correctly inserted into the text of the RichTextBox control.

How to Create the Demonstration Program

The demonstration program below shows how to insert a TAB control characterin the RichTextBox control provided in Visual Basic.
  1. Create a new project in Visual Basic. Form1 is created by default.
  2. Add a RichTextBox control to Form1.
  3. Add the following code to Form1:
          Dim arrTabStop() As Boolean      Private Sub RichTextBox1_GotFocus()            'Store the TabStop property for each control on the            'form and then set the TabStop property of each            'control to False            ReDim arrTabStop(0 To Controls.Count - 1) As Boolean            For i = 0 To Controls.Count - 1               arrTabStop(i) = Controls(i).TabStop               Controls(i).TabStop = False            Next      End Sub      Private Sub RichTextBox1_LostFocus()            'Restore the Tabstop property for each control on the form            For i = 0 To Controls.Count - 1               Controls(i).TabStop = arrTabStop(i)            Next      End Sub						
  4. Add two Command Buttons control to Form1.
Execute the demonstration program by pressing the F5 function key. Thefocus is set to the RichTextBox control. Type some text into theRichTextBox control. Whenever desired, you can press the TAB key to insertthat control character into the text you are typing. Notice that pressingTAB does not move the focus to the Command Button control; you must clickthe mouse pointer on the command button itself to move the focus to it.
kbVBp400 kbVBp500 kbVBp600 kbVBp kbdsd kbDSupport kbControl
Properties

Article ID: 143273 - Last Review: 07/01/2004 21:34:00 - Revision: 2.1

  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 6.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic 6.0 Enterprise Edition
  • Microsoft Visual Basic 4.0 Standard Edition
  • Microsoft Visual Basic 4.0 Professional Edition
  • Microsoft Visual Basic 4.0 32-Bit Enterprise Edition
  • kbhowto KB143273
Feedback