* VB-CODE (1)
Tip 145: Inserting Tab Characters in the Rich-Text Box Control
August 31, 1995
The Rich-Text Box control allows you to create rich-text format (RTF)
from within your Microsoft® Visual Basic® application. However, when
you want to
insert a tab character in the Rich-Text Box control, the focus is
to the next control in the tab order specified by the TabIndex
article explains how you can insert the tab character into the
Setting a Control's Tab Order
At run time, a Microsoft® Visual Basic® user must press CTRL+TAB to
insert a tab
character in a Rich-Text Box control. However, most people are
pressing the TAB key. Whenever the TAB key is pressed from within a
Box control, the focus is immediately set to the next control on the
TabIndex property of a control determines which control then receives
This is not the effect you want.
When designing a form in Visual Basic, you can add controls such as
Buttons and Text Boxes to perform functions within your application.
you add a new control to a form, Visual Basic assigns a new value to
control. This value is saved in the control's TabIndex property. At run
a user can press the TAB key to move the focus from one control to
focus is moved to the control that has the next highest TabIndex value.
You can change the value of a control's TabIndex property either during
time or at run time. However, the control must have a TabStop property
with it. The TabStop property determines whether a user can press the
TAB key to
set the focus to that specific control.
In the example program below, the TabStop property of all controls on
is set to False. This prevents the user from setting the focus to
by using the TAB keyeven though the Rich-Text Box control has the
focus. In this
way, the Tab control character is correctly inserted into the text of
Rich-Text Box control.
This program shows how to insert a tab control character in the
control in Visual Basic version 4.0.
1. Create a new project in Visual Basic. Form1 is created by default.
2. Add a Rich-Text Box control to Form1. RichTextBox1 is created by
3. Add the following code to the GotFocus event for RichTextBox1.
Private Sub RichTextBox1_GotFocus()
On Error Resume Next
For Each Control In Controls
Control.TabStop = False
4. Add a Command Button control to Form1. Command1 is created by
Run the example program by pressing F5. Note that the focus is set to
Rich-Text Box control. Type some text into this control. You can press
TAB key to insert that control character into the text you are typing
necessary. You should also note that pressing TAB does not move the
the Command Button controlyou must click the Command Button itself to
focus to it. In other words, while the focus is set to the Rich-Text
you can press the TAB key and that character is inserted into the
"TabStop Property." (Development Library, Product Documentation,
Visual Basic 4.0 Professional and Enterprise Editions, Language
"Removing a Control from the Tab Order." (Development Library,
Product Documentation, Languages, Visual Basic 4.0 Professional and
Enterprise Editions, Programmer's Guide, Chapter 3 Creating and
Setting the Tab Order)