i want to disable some keys on the keyboard
when a user starts entering text into a text box.
will it be posiible
if so how
Printable View
i want to disable some keys on the keyboard
when a user starts entering text into a text box.
will it be posiible
if so how
why don't you just do something like this:
just add the keys that you don't want them to press
if text1.text ="1"
then exit sub
If you just want to control what they type into a text box, then you can just trap the keys you don't want.
e.g. if you just want numbers and ':', do this:
VB Code:
If InStr("0123456789:", Chr$(KeyAscii)) = 0 And KeyAscii <> 8 Then Beep KeyAscii = 0 End If
(KeyAscii 8 is the BackSpace key, which you probably want to allow.)
Note that this will not stop a user from pasting a string, so you may want to check the text box in its LostFocus event.
You might also want to take a look at the masked edit box that comes with VB. It's fully documented in VB.
the code works fine but it does not for tab key.
i want to the tabkey does not work for my textbox.
if pressess tab key it should not focus the next text box
That's not so easy.
You can't trap the Tab key with KeyDown or KeyPress.
You will have to use the GetKeyState API to see if it has been pressed.
VB Code:
Option Explicit Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer Private Sub Text1_Validate(Cancel As Boolean) Dim RetVal As Integer RetVal = GetKeyState(vbKeyTab) If RetVal < 0 Then Beep Cancel = True End If End Sub
You can set the TabStop property to False on each control on the form when the TextBox get the focus and turn them back when it lose the focus.Best regardsVB Code:
Private Sub Text1_GotFocus() Dim ctl As Control On Error Resume Next For Each ctl In Me.Controls ctl.TabStop = False Next End Sub Private Sub Text1_LostFocus() Dim ctl As Control On Error Resume Next For Each ctl In Me.Controls ctl.TabStop = True Next End Sub