VB - A demonstration of how to trap the Tab key-VBForums
Results 1 to 5 of 5

Thread: VB - A demonstration of how to trap the Tab key

Hybrid View

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    32,851

    VB - A demonstration of how to trap the Tab key

    Normally the pressing of the Tab key can not trapped if there is more than one control on a form that has a TabStop set to True. The attached form demonstrates how to get around that problem.
    Attached Files Attached Files

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Just to note one thing. Before assigning the TabStop property to False, first check if the control is not Label, Image or Line. These controls don't have TabStop property because they are being painted directly on the form.

  3. #3

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    32,851
    Thanks Serge. The StoreTabsStops sub in the form has the following statement:

    VB Code:
    1. 'Store the TabStop property for each control on the form. If you run into
    2.     'a control without a TabStop, just add an On Error Resume line
    I probably should have just included the On error Resume Next line instead of the advice.

  4. #4
    Let me in .. techyspecy's Avatar
    Join Date
    Aug 2002
    Location
    Back to VBF.
    Posts
    2,456
    Here is another way ..
    Put two textboxes on the form ..

    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    4. Private Const VK_TAB = &H9
    5.  
    6. Private Sub Text2_LostFocus()
    7.     If GetTabState Then MsgBox "You just tabbed out of Text2"
    8. End Sub
    9.  
    10. Private Sub Text1_LostFocus()
    11.     If GetTabState Then MsgBox "You just tabbed out of Text1"
    12. End Sub
    13.  
    14. Private Function GetTabState() As Boolean
    15.     GetTabState = False
    16.     If GetKeyState(VK_TAB) And -256 Then
    17.         GetTabState = True
    18.     End If
    19. End Function

    Simple, Effective and Efficient code ...

  5. #5

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    32,851
    This thread got away from what I think is appropriate for the CodeBank so rather than closing it I deleted several posts. I also edited out of techyspecy's post a quote that referred to the deleted posts. If you have concerns about my having taken this action please PM me.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.