This sort of stuff is a nightmare.
One would have thought that setting a Form's KeyPreview to True would
do just that - but it does not - some controls steal certain keys
regardless.
You also have a circular problem - if you want to disable the Tab key
then you will still have it disabled when you send in A TAB KEY using
SendKeys.
Here is a sample of 'burning off keys' to prevent beeps
(more after sample)
Option Explicit
' Start with two Textboxes
' Add other controls to test
Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyTab: Me.Print "Form Tab": KeyCode = 0
Case vbKeyReturn: Me.Print "Form Return": KeyCode = 0
Case vbKeyUp: Me.Print "Form Up": KeyCode = 0
Case Else: Me.Print KeyCode
End Select
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKeyTab: Me.Print "Form Tab": KeyAscii = 0
Case vbKeyReturn: Me.Print "Form Return": KeyAscii = 0
Case vbKeyUp: Me.Print "Form Up"
Case Else: Me.Print KeyAscii
End Select
End Sub
Private Sub Form_Load()
Dim C As Control
Me.KeyPreview = True
For Each C In Controls
C.TabStop = False
Next
End Sub
=========
You will find that Textboxes are just about manageable - but
Commandbuttons a nightmare.
In fact this key stealing stuff is by design - Windows sends a message
to the control with focus ( WM_GetDlgCode ) and if it replies with :-
DLGC_WANTARROWS or DLGC_WANTTAB or DLGC_WANTALLKEYS
Then the control gets all keys.
However this involves a lot of sub classing and is a pain to implement
in VB
Quote:
>Hi,
>in my form, I want to disable the TAB key.
>I have tried to do this using the ascii code for vbTAB (9), but it
>does'nt recognize this at all.
>At the same time, I am simulating the TAB key Using the ENTER key by
>doing:
>if keyascii=13 then sendkeys vbtab.
>This is working fine, but it does produce an audible signal (beep)
>How can I turn this off?
>thx,
>James