Results 1 to 7 of 7

Thread: question on key hit and focus

  1. #1

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    question on key hit and focus

    Is there a way to tell which control has the focus?

    I want to pass a control to a specific function only if it has focus.

    Also my second question is...I want to use F7 as a "Sendkeys" or keydown event..but how do I check if this button is clicked.

    I want it at the form level...
    basically user hits F7 .. and the text box that has the focus calls a specific function that takes as a parameter that control.

    THanks,
    Jon

  2. #2

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492

    Re: question on key hit and focus



    Err..I need a hasfocus..or an API that tells me what text box / control has the focus..

    I took the majority of this from marty to spell check a box...

    VB Code:
    1. Public Function CheckSpelling(t As TextBox)
    2. On Error GoTo Err_Handler
    3.  
    4.     Dim objWord As Object
    5.     Dim objDoc  As Object
    6.     Dim strResult As String
    7.    
    8.     'Create a new instance of word Application
    9.     If (Len(t.Text) = 0) Then
    10.         'nahhhhhhhhhhh
    11.     Else
    12.         Set objWord = CreateObject("word.Application")
    13.  
    14.         Select Case objWord.Version
    15.             'Office 2000, xp, 2k3
    16.                 Case "9.0", "10.0", "11.0"
    17.                 Set objDoc = objWord.Documents.Add(, , 1, True)
    18.             'Office 97
    19.                 Case Else
    20.                 Set objDoc = objWord.Documents.Add
    21.         End Select
    22.  
    23.         objDoc.Content = t.Text
    24.         objDoc.CheckSpelling
    25.  
    26.         strResult = Left(objDoc.Content, Len(objDoc.Content) - 1)
    27.  
    28.         If t.Text = strResult Then
    29.             ' There were no spelling errors, so give the user a
    30.             ' visual signal that something happened
    31.             MsgBox "The spelling check is complete.", vbInformation + vbOKOnly, "Spelling Complete"
    32.         End If
    33.    
    34.         'Clean up
    35.         objDoc.Close False
    36.         Set objDoc = Nothing
    37.         objWord.Application.Quit True
    38.         Set objWord = Nothing
    39.  
    40.         ' Replace the selected text with the corrected text. It's important that
    41.         ' this be done after the "Clean Up" because otherwise there are problems
    42.         ' with the screen not repainting
    43.         t.Text = strResult
    44.     End If
    45.  
    46. Done:
    47. Exit Function
    48.  
    49. 'in case user does not have word...
    50. Err_Handler:
    51. MsgBox Err.description & Chr(13) & Chr(13) & "Please note you must have Microsoft Word installed to utilize the spell check feature.", vbCritical, "Error #: " & Err.Number
    52. Resume Done
    53.  
    54. End Function

    But I want to call it and pass the text box that has the focus via say F7 key click. Right now Im not sure how to do this though

    Anyone with some ideas?

  3. #3

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Err...

    So I can loop through the controls to find the text box...
    but I need to be able to have an AND condition in the if to see if the text box has focus.

    I only want to spell check the textbox that has the focus ONLY!!!

    VB Code:
    1. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    2. On Error GoTo Err_Handler
    3.  
    4. Dim ctl As Control
    5.  
    6. If KeyCode = vbKeyF7 Then
    7.     'let's check each control
    8. For Each ctl In Me.Controls
    9.    'is it a button
    10.    If TypeOf ctl Is TextBox Then
    11.         Call CheckSpelling(ctl)
    12.         Exit For
    13.    End If
    14. Next ctl
    15. Else
    16.     'do nothing
    17. End If
    18.  
    19. Done:
    20. Exit Sub
    21.  
    22. Err_Handler:
    23. MsgBox Err.description, vbCritical, "Error #: " & Err.Number
    24. Resume Done
    25.  
    26. End Sub

    ...Need to modify

    if TypeOf ctl is TextBox AND hasfocus!!!

    Any ideas ?

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    Have you tried form.activecontrol??

  5. #5

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by szlamany
    Have you tried form.activecontrol??
    Me no like that..but it may be me only option

    Shanks,

  6. #6

  7. #7

    Thread Starter
    Banned jhermiz's Avatar
    Join Date
    Jun 2002
    Location
    Antarctica
    Posts
    2,492
    Originally posted by MartinLiss
    Why not? It's a legitimate part of the language.
    My mistake..

    I was using the formName.ActiveControl and thought I'd have to do that for each form...then I realized you can use Me.

    Works fine..see my post in your spell checker.

    Jon

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