Inserting TAB Characters in RichTextBox Control in VB

Last reviewed: May 20, 1996
Article ID: Q143273
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 32-bit editions, for Windows, version 4.0

SUMMARY

The RichTextBox control allows you to create RTF documents from within your Visual Basic program. However, when you want to insert a TAB character in the RichTextBox control, the focus is instead moved to the next control in the tab order specified by the TabIndex property. This article explains how you can insert the TAB character into the RichTextBox control itself.

MORE INFORMATION

SETTING A CONTROL'S TAB ORDER

At run time, a user must press CTRL+TAB to insert a TAB character in a RichTextBox control. However, most people are accustomed to just simply pressing the TAB key. Whenever the TAB key is pressed from within a RichTextBox control, the focus is immediately set to the next control on the form. The TabIndex property of a control determines which control, then receives the focus.

When designing a form in Visual Basic, you add controls such as command buttons and text boxes to perform functions within your application. Each time you add a new control to a form, Visual Basic assigns a new value to that control. This value is saved in the control's TabIndex property. During run time, a user can press the TAB key to move the focus from one control to another. The 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 design time or at run time. However, the control must have a TabStop property associated with it. The TabStop property determines whether a user can press the TAB key to set the focus to that specific control.

In the demonstration program below, we change the TabStop property of all controls 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 is because the TabStop property is set to False. Therefore, the TAB control character 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 character in the RichTextBox control provided in Visual Basic 4.0.

  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 the GotFocus event for RichTextBox1.

       Private Sub RichTextBox1_GotFocus()
           On Error Resume Next
           For Each Control In Controls
               Control.TabStop = False
           Next Control
       End Sub
    
    

  4. Add a Command Button control to Form1.

Execute the demonstration program by pressing the F5 function key. The focus is set to the RichTextBox control. Type some text into the RichTextBox control. Whenever desired, you can press the TAB key to insert that control character into the text you are typing. Notice that pressing TAB does not move the focus to the Command Button control--you must click the mouse pointer on the command button itself to move the focus to it. In other words, while the focus is set to the RichTextBox control, you can press the TAB key and that character is inserted into the RichTextBox control.

Additional References

Product Documentation, Office Developer's Kit, Visual Basic 3.0 Professional Edition. TabStop Property. Product Documentation, Office Developer's Kit, Visual Basic 3.0 Professional Edition, Programmer's Guide. Removing a Control from the Tab Order.


Additional reference words: 4.00 vb4win vb432
KBCategory: kbprg
KBSubcategory: PrgCtrlsCus



THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: May 20, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.