How to Use TABs in a VB Text Box Without Changing the Focus

This article was previously published under Q109261
This article has been archived. It is offered "as is" and will no longer be updated.
SUMMARY
This article shows by example how to use the TAB keypress within a control,such as a text box. Normally, the TAB key causes the focus to move awayfrom that control. The sample program in this article shows you how tochange this behavior so you can use the TABs within a text box.

The sample program does this by setting the TabStop property of allcontrols on the form to False when the text box has the focus. Tabbingchanges focus between any controls which have a TabStop property equal toTrue, which is the default. When the TabStop property is true for one ormore controls on a form, Visual Basic does not allow tabs to be entereddirectly into a control.
MORE INFORMATION

Step-by-Step Example

In the example below, the Text2 box will accept and hold TAB keystrokes,keeping them in the Text property along with the other entered characters.The Text1 and Text3 boxes will not accept TAB keystrokes. When Text1 andText3 have the focus, pressing the TAB key changes the focus to the nextcontrol in the tab order.
  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add three text boxes (Text1, Text2, and Text3) to Form1. Select the Text2 box and press the F4 key to display the Properties window. Set the MultiLine property of Text2 to True.

    NOTE: When you press the TAB key, single-line text boxes beep and do not accept the TAB keystroke, but multiLine text boxes do accept TAB keystrokes.
  3. Double-click the Text2 box to open the code window. Choose the GotFocus event from the Proc box. Add the following code to the Text2 GotFocus event:
       Sub Text2_GotFocus ()      ' When Text2 gets the focus, clear all TabStop properties on all      ' controls on the form. Ignore all errors, in case a control does      ' not have the TabStop property.      On Error Resume Next      For i = 0 To Controls.Count - 1   ' Use the Controls collection         Controls(i).TabStop = False      Next   End Sub						
    NOTE: See the "Controls Collection" section below for an explanation of the Controls collection.
  4. Choose the LostFocus event from the Proc box. Add the following code to the Text2 LostFocus event:
       Sub Text2_LostFocus ()      ' When Text2 loses the focus, make the TabStop property True for all      ' controls on the form. That restores the ability to tab between      ' controls. Ignore all errors, in case a control does not have the      ' TabStop property.      On Error Resume Next      For i = 0 To Controls.Count - 1   ' Use the Controls collection         Controls(i).TabStop = True      Next   End Sub						
  5. Start the program, or press the F5 key. Press the TAB key to give focus to Text2. Enter text into the Text2 box, pressing the TAB key as needed. Whenever Text1 or Text3 has the focus, pressing the TAB key moves the focus to the next control. Whenever Text2 has the focus, TAB keystrokes remain with the text in the text box. Close the form to end the program.

Tab Order

By default, Visual Basic assigns tab order to controls in the order youdraw them on a form. Each new control is placed last in the tab order. Youcan control the order that controls gain focus in your application bychanging the tab order at design time through the Properties window, or atrun time through code.

To change tab order at design time:
  1. Click a control to select it.
  2. From the Properties window, select TabIndex. Visual Basic displays the current tab position in the Settings box.
  3. Type the number for the tab order position you want the control to have.
  4. Click the Enter button. You can test the tab order at design time by pressing Tab.
To enable or disable a tab stop at design time:
  1. Click a control to select it.
  2. From the Properties window, select TabStop. Visual Basic displays the current Boolean value in the Settings box.
  3. Select True to designate the control as a tab stop, or select False to bypass the control in the tab order.
  4. Click the Enter button.
When you change a control's tab order position, Visual Basic automaticallyrenumbers the tab order positions of other controls to reflect insertionsand deletions.

A control whose TabStop property has been set to False maintains itsposition in the actual tab order as set by the TabIndex property, eventhough the control is skipped when you cycle through the controls by usingthe TAB key. If the TabStop property is False for all controls on the form,you can enter TAB keystrokes into MultiLine text boxes.

Controls Collection

The Controls collection is a collection whose elements represent eachcontrol on a form, including elements of control arrays. The Controlscollection has a single property (Count) that specifies the number ofelements in an array.

The Controls collection enumerates loaded controls on a form and is usefulfor iterating through them. The index in the syntax is between 0 andControls.Count-1.

NOTE: Controls is a keyword but not a reserved word. It identifies anintrinsic form-level variable named Controls. If you omit the optional formreference, you must include the Controls keyword. If you include a formreference, you can omit the Controls keyword. For example, the followingtwo lines have the same effect:

MyForm.Controls(6).Top = MyForm.Controls(5).Top + incrementMyForm(6).Top = MyForm(5).Top + increment
3.00
Properties

Article ID: 109261 - Last Review: 12/04/2015 09:53:39 - Revision: 2.0

Microsoft Visual Basic 3.0 Professional Edition, Microsoft Visual Basic 3.0 Professional Edition

  • kbnosurvey kbarchive KB109261
Feedback