Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW DO I DISABLE USE OF TAB KEY TO USER 3

Status
Not open for further replies.

TNN

Programmer
Sep 13, 2000
417
0
0
US
How do I disable the use of the tab key to the user yet allow the user to enter text in a text box. I want the user to leave the text box with the enter key but when the user uses the tab key nothing happens.

The enter key now moves the user to the appropriate next text box. Don't want user to be able to use tab key.

Thank You for any help.
TNN, Tom
TNPAYROLL@AOL.COM

TOM
 
Disable the TabStop property of all the controls on your form and nothing will happen on tab. And the tab key will now raise the KeyPress event !

CU
Tom
 
TomB,
Can't disable the TabStop property because I'm causing the SendKeys "{TAB}" from the enter key. Want user to be able to use the more accessible enter key. The keyPress event of the text box uses the SendKeys "{TAB}" to tab thru the text boxes and have also included edits to skip over some text boxes.
Thank You for your response.
TNN, Tom
TNPAYROLL@AOL.COM

TOM
 
If I remember correctly I used to use something like:

Private Sub CtrlObject_KeyPress(KeyAscii as Integer)

If KeyAscii = vbKeyTab Then
KeyAscii = 0
'Additional processing goes here
Endif

End sub

'Note: Settign KeyAscii to 0 will prevent VB from processing the character entirely.Works for KeyPress event but not for KeyUp or KeyDown events in many scenarios.

Hope this quick one helps.
 
Ehem, I need to correct myself here...Disregard my previous post, it won't work for the reason stated below (in regards to the TAB key)...This is somewhat contradictory since I remember doing it in a similar fashion some time ago and Microsoft Knowledge base states that KeyPress event could and should be used for trapping TAB key...
The following is an excerpt from MSKB
-----------------------------------------------------------

Writing Form-Level Keyboard Handlers


Each KeyDown and KeyUp event is attached to a specific object. To write a keyboard handler that applies to all objects on the form, set the KeyPreview property of the form to True. When the KeyPreview property is set to True, the form recognizes the KeyPress, KeyUp, and KeyDown events for all controls on the form before the controls themselves recognize the events. This makes it very easy to provide a common response to a particular keystroke.

You can set the KeyPreview property of the form to True in the Properties window or through code in the Form_Load procedure:

Private Sub Form_Load
Form1.KeyPreview = True
End Sub

You can test for the various key states on a form by declaring a ShiftKey variable and using the Select Case statement. The following procedure will print the message to the form regardless of which control has the focus.

Open a new project and add the variable ShiftKey to the Declarations section of the form:

Dim ShiftKey as Integer

Add a Textbox and a CommandButton control to the form. Add the following procedure to the form's KeyDown event:

Private Sub Form_KeyDown(KeyCode As Integer, _
Shift As Integer)
ShiftKey = Shift And 7
Select Case ShiftKey
Case 1 ' or vbShiftMask
Print "You pressed the SHIFT key."
Case 2 ' or vbCtrlMask
Print "You pressed the CTRL key."
Case 4 ' or vbAltMask
Print "You pressed the ALT key."
End Select
End Sub

If you have defined a shortcut key for a menu control, the Click event for that menu control occurs automatically when the user types that key, and no key event occurs.

Similarly, if there is a command button on the form with the Default property set to True, the ENTER key causes the Click event for that command button to occur instead of a key event. If there is a command button with the Cancel property set to True, the ESC key causes the Click event for that command button to occur instead of a key event.

For example, if you add a Click event procedure to the CommandButton and then set either the Default or Cancel properties to True, pressing the RETURN or ESC keys will override the KeyDown event. This procedure closes the application:

Private Sub Command1_Click()
End
End Sub

Notice that the TAB key moves the focus from control to control and does not cause a key event unless every control on the form is disabled or has TabStop set to False.

When the KeyPreview property of the form is set to True, the form recognizes the keyboard events before the controls, but the events still occur for the controls. To prevent this, you can set the keyascii or keycode arguments in the form key-event procedures to 0. For example, if there is no default button on the form, you can use the ENTER key to move the focus from control to control:

Private Sub Form_KeyPress (KeyAscii As Integer)
Dim NextTabIndex As Integer, i As Integer
If KeyAscii = 13 Then
If Screen.ActiveControl.TabIndex = _
Count - 1 Then
NextTabIndex = 0
Else
NextTabIndex = Screen.ActiveControl._
TabIndex + 1
End If
For i = 0 To Count - 1
If Me.Controls(i).TabIndex = _
NextTabIndex Then
Me.Controls(i).SetFocus
Exit For
End If
Next i
KeyAscii = 0
End If
End Sub

Because this code sets keyascii to 0 when it is 13, the controls never recognize the ENTER key being pressed, and their key-event procedures are never called.
 
TNN,

try this:

Set all tabstop properties false.
Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then
KeyAscii = 0
TabIndex = Form1.ActiveControl.TabIndex + 1
Form1.Controls(TabIndex).SetFocus
End If
End Sub

You don't need the sendkeys. Use the tabindex property to move around.

Hope that helps ....

Tom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top