Results 1 to 6 of 6

Thread: disable key

  1. #1

    Thread Starter
    Lively Member muralidhary's Avatar
    Join Date
    Aug 2001
    Location
    Chennai
    Posts
    89

    Unhappy disable key

    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
    ENJOY

  2. #2
    New Member
    Join Date
    Sep 2001
    Posts
    14

    different idea

    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

  3. #3
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796
    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:
    1. If InStr("0123456789:", Chr$(KeyAscii)) = 0 And KeyAscii <> 8 Then
    2.     Beep
    3.     KeyAscii = 0
    4.   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.
    Last edited by BrianHawley; Sep 24th, 2001 at 03:24 AM.
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  4. #4

    Thread Starter
    Lively Member muralidhary's Avatar
    Join Date
    Aug 2001
    Location
    Chennai
    Posts
    89

    hai

    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
    ENJOY

  5. #5
    Fanatic Member BrianHawley's Avatar
    Join Date
    Aug 2001
    Location
    Saudi Arabia
    Posts
    796
    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:
    1. Option Explicit
    2. Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    3.  
    4.  
    5. Private Sub Text1_Validate(Cancel As Boolean)
    6.    
    7.   Dim RetVal As Integer
    8.  
    9.   RetVal = GetKeyState(vbKeyTab)
    10.    
    11.   If RetVal < 0 Then
    12.     Beep
    13.     Cancel = True
    14.   End If
    15.    
    16. End Sub
    Brian
    (Fighting with the RightToLeft bugs in VS 2005)

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    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.
    VB Code:
    1. Private Sub Text1_GotFocus()
    2.     Dim ctl As Control
    3.     On Error Resume Next
    4.     For Each ctl In Me.Controls
    5.         ctl.TabStop = False
    6.     Next
    7. End Sub
    8.  
    9. Private Sub Text1_LostFocus()
    10.     Dim ctl As Control
    11.     On Error Resume Next
    12.     For Each ctl In Me.Controls
    13.         ctl.TabStop = True
    14.     Next
    15. End Sub
    Best regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width