Results 1 to 13 of 13

Thread: How to determine what control has focus

  1. #1

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519

    Question How to determine what control has focus

    Is there an easy way to check to see what control on a form has focus and lost focus?

    Example:

    I have a form with 10 textboxes.

    in each textbox "GotFocus" event I have this code..
    (keep in mind the "text." name changes in each event)
    VB Code:
    1. text.backcolor = vbred
    2. doevents

    and in each textbox "LostFocus" event I have this code...
    VB Code:
    1. text.backcolor = vbwhite
    2. doevents

    I would like to have 2 events like ControlGotFoucs and ControlLostFocus and use some type of case statment (or something else) to change the backcolor for the controls...

    Is this possible or do I need to code each and every controls events?

    Thanks for the info,
    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    If you use a Control Array you could just use one copy of the code.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Lively Member blaff's Avatar
    Join Date
    Nov 2002
    Location
    Germany
    Posts
    69
    If you make a control array of all your textboxes you would have only one event proc!

    (plenderj replied faster: go get a live )
    _____
    blaff

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Originally posted by blaff
    (plenderj replied faster: go get a live )

    :P
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    Yea, a control array would be great but this project has way to many controls to go back and change them all to an array. I was hoping for some kind of an event that is triggered when a control gets/looses focus other than the one that I mentioned above.

    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  6. #6
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    It's not hard to add them all to a control array:
    • Select one of the TextBoxes (the "first" one, if you care about the order of their indexes)
    • Give it a more generic name that is appropriate for all the textboxes (like instead of txtFirstName, make it txtCustInfo)
    • Copy that name from the Property window
    • With the Name property for the TextBox control still highlighted in the Property window, select the next TextBox and Paste (Ctrl+V)
    • Say "Yes" to the "do you want to make a control array" dialog (only happens once)
    • Then just continue with the rest of the textboxes, selecting a textbox and pasting.
    Should take less than a minute for 100 controls (depending on how long it takes you to Left Click, Ctrl+V)

    The more painful part would be if you have more specific code written already for data validation that references the TextBoxes by name (instead of index). This would require module level text replacement (Replace all instances of txtFirstName with txtCustInfo(0), for each TextBox).
    ~seaweed

  7. #7
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    You could install a thread local WH_CBT hook and trap for the hcbt_focuschanged which is triggered whenever focus shifts from one control to another in your app.
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  8. #8
    Lively Member blaff's Avatar
    Join Date
    Nov 2002
    Location
    Germany
    Posts
    69
    You can make your own textbox control, which makes its own bkg red after getting the focus.
    But this way you would have to replace all textboxes with your own textbox...
    _____
    blaff

  9. #9
    Frenzied Member MerrionComputin's Avatar
    Join Date
    Apr 2001
    Location
    Dublin, Ireland
    Posts
    1,616
    To do the WH_CBT thing with the EventVB.dll:

    VB Code:
    1. Option Explicit
    2.  
    3. Dim WithEvents vbLink As EventVB.APIFunctions
    4. Dim WithEvents vbHook As EventVB.ApiSystemHook
    5.  
    6. Private Sub Form_Load()
    7.  
    8. Set vbLink = New APIFunctions
    9.  
    10. Set vbHook = vbLink.System.Hooks
    11. vbHook.StartHook WH_CBT, HOOK_LOCAL_PROCESS
    12.  
    13. End Sub
    14.  
    15. Private Sub vbHook_BeforeFocusChanged(ByVal GainFocus As EventVB.ApiWindow, ByVal LoseFocus As EventVB.ApiWindow, Cancel As Boolean)
    16.  
    17. Debug.Print "Window " & GainFocus.hWnd & " gains focus from " & LoseFocus.hWnd
    18.  
    19. End Sub
    ----8<---------------------------------------
    NEW - The .NET printer queue monitor component
    ----8<---------------------------------------
    Now with Examples of use

  10. #10

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    Originally posted by MerrionComputin
    To do the WH_CBT thing with the EventVB.dll:

    VB Code:
    1. Option Explicit
    2.  
    3. Dim WithEvents vbLink As EventVB.APIFunctions
    4. Dim WithEvents vbHook As EventVB.ApiSystemHook
    5.  
    6. Private Sub Form_Load()
    7.  
    8. Set vbLink = New APIFunctions
    9.  
    10. Set vbHook = vbLink.System.Hooks
    11. vbHook.StartHook WH_CBT, HOOK_LOCAL_PROCESS
    12.  
    13. End Sub
    14.  
    15. Private Sub vbHook_BeforeFocusChanged(ByVal GainFocus As EventVB.ApiWindow, ByVal LoseFocus As EventVB.ApiWindow, Cancel As Boolean)
    16.  
    17. Debug.Print "Window " & GainFocus.hWnd & " gains focus from " & LoseFocus.hWnd
    18.  
    19. End Sub


    MerrionComputin - This is what I was looking for... Thanks! I will try this out and see how it turns out.

    Thanks guys! -
    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  11. #11

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    MerrionComputin,

    I got the dll, looks nice. I have a couple of questions..

    when exiting the program I need to add the folowing code Correct?

    VB Code:
    1. Set vbHook = Nothing
    2. Set vbLink = Nothing

    Next,

    How do I translate that number (GainFocus.hWnd and LooseFocus.hWnd) into the control name?

    Thanks for the help,
    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  12. #12
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    Here's a non-API solution which uses some basic Class functionality:

    Class Module: (clsTextbox)
    VB Code:
    1. Option Explicit
    2.  
    3. Private WithEvents moTextBox As TextBox
    4. Private moBoxes As clsTextBoxes
    5.  
    6. Private Sub Class_Terminate()
    7.   UnBindTextBox
    8. End Sub
    9.  
    10. Friend Sub BindTextBox(ByRef TextBoxControl As TextBox, ByRef Boxes As clsTextBoxes)
    11.   Set moBoxes = Boxes
    12.   Set moTextBox = TextBoxControl
    13. End Sub
    14.  
    15. Friend Sub UnBindTextBox()
    16.   Set moTextBox = Nothing
    17.   Set moBoxes = Nothing
    18. End Sub
    19.  
    20. Private Sub moTextBox_GotFocus()
    21.   moBoxes.Fire_GotFocus moTextBox
    22. End Sub
    23.  
    24. Private Sub moTextBox_LostFocus()
    25.   moBoxes.Fire_LostFocus moTextBox
    26. End Sub
    Class Module: (clsTextBoxes)
    VB Code:
    1. Option Explicit
    2.  
    3. Public Event GotFocus(ByRef TextBoxControl As TextBox)
    4. Public Event LostFocus(ByRef TextBoxControl As TextBox)
    5.  
    6. Private moBoxes As Collection
    7.  
    8. Private Sub Class_Initialize()
    9.   Set moBoxes = New Collection
    10. End Sub
    11.  
    12. Private Sub Class_Terminate()
    13.   Dim oBox As clsTextBox
    14.  
    15.   For Each oBox In moBoxes
    16.     oBox.UnBindTextBox
    17.   Next
    18.   Set moBoxes = Nothing
    19. End Sub
    20.  
    21. Public Sub Add(ByRef TextBoxControl As TextBox)
    22.   Dim oBox As clsTextBox
    23.  
    24.   Set oBox = New clsTextBox
    25.  
    26.   oBox.BindTextBox TextBoxControl, Me
    27.  
    28.   moBoxes.Add oBox
    29. End Sub
    30.  
    31. Friend Sub Fire_GotFocus(ByRef TextBoxControl As TextBox)
    32.   RaiseEvent GotFocus(TextBoxControl)
    33. End Sub
    34.  
    35. Friend Sub Fire_LostFocus(ByRef TextBoxControl As TextBox)
    36.   RaiseEvent LostFocus(TextBoxControl)
    37. End Sub
    Example Usage:
    VB Code:
    1. Option Explicit
    2.  
    3. Private WithEvents TextBoxes As clsTextBoxes
    4.  
    5. Private Sub Form_Load()
    6.   Dim oControl As Control
    7.  
    8.   Set TextBoxes = New clsTextBoxes
    9.  
    10.   For Each oControl In Me
    11.     If TypeOf oControl Is TextBox Then
    12.       TextBoxes.Add oControl
    13.     End If
    14.   Next
    15. End Sub
    16.  
    17. Private Sub Form_Unload(Cancel As Integer)
    18.   Set TextBoxes = Nothing
    19. End Sub
    20.  
    21. Private Sub TextBoxes_GotFocus(TextBoxControl As TextBox)
    22.   TextBoxControl.BackColor = vbRed
    23. End Sub
    24.  
    25. Private Sub TextBoxes_LostFocus(TextBoxControl As TextBox)
    26.   TextBoxControl.BackColor = vbWindowBackground
    27. End Sub

  13. #13

    Thread Starter
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    Arron,

    That was very good. It worked perfectly for what I needed.

    Thanks a million!

    Rudy

    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

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