Results 1 to 11 of 11

Thread: Find what control has focus?

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Find what control has focus?

    Is there a way to find out which control has focus in vb? thanks

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Find what control has focus?

    Use the ActiveControl property of the form, eg:

    MsgBox Me.ActiveControl.Name

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find what control has focus?

    Is there any way to find the last used control also? If not, can i use a string to hold its name, and use CallByName function to call the sub?

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Find what control has focus?

    There isnt a property or anything else to tell you the "last used" control. I guess what you could do is use a timer to check the ActiveControl, then update a variable when it changes. (instead of the timer you could write code in all LostFocus events!)

  5. #5

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find what control has focus?

    Instead, i tried to use something like:
    VB Code:
    1. For Each Ctl In Me
    and compare it with the string, but its only working, to the fact it doesnt work for control arrays. Is that the best way to go about it you think?

  6. #6
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: Find what control has focus?

    You can declare an object variable 'withevents' and set it to the active control.
    When focus changes the last active control will raise it's lostfocus event and you can catch through the variable.
    This way you only need to code one lostfocus event.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Find what control has focus?

    That's a very good idea!

  8. #8

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Find what control has focus?

    ive never used withevents, although i have read about it. I will do some more research and report back once i figure this out

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Find what control has focus?

    Here is some code you can use. Create a form with a Text1 control array, a listbox and a command button.

    VB Code:
    1. Option Explicit
    2. Dim mctlLast As Control
    3.  
    4. Private Sub Command1_Click()
    5.  
    6.     MsgBox "The last control was " & mctlLast.Name
    7.    
    8.     On Error Resume Next
    9.     If mctlLast.Index >= 0 Then
    10.         MsgBox "And it's index is " & mctlLast.Index
    11.     End If
    12.    
    13. End Sub
    14.  
    15. Private Sub Command1_LostFocus()
    16.  
    17.     Set mctlLast = Command1
    18.  
    19. End Sub
    20.  
    21. Private Sub List1_LostFocus()
    22.  
    23.     Set mctlLast = List1
    24.  
    25. End Sub
    26.  
    27.  
    28. Private Sub Text1_LostFocus(Index As Integer)
    29.  
    30.     Set mctlLast = Text1(Index)
    31.  
    32. End Sub

  10. #10
    New Member
    Join Date
    Apr 2010
    Posts
    4

    Re: Find what control has focus?

    HI All,
    I have 4 command buttons commandbutton1 - 4 on my excel 'Sheet8'.
    I want Bold-Font for whichever of the buttons 1 - 3 get focus.
    Button 4 is just a settings button that doesn't need focus, it basically Opens up a UserForm when clicked.

    I defined LostFocus() for buttons 1-3 and they work fine. They basically sort my columns for some names.

    Now, when i click button 1, it gets focus, Bolds out its font, sorts out my columns on name "GGG". BUttons 2,3 work the same. But when i click button 4:

    1) Last Button from 1-3 looses focus, UnBolds its font.(Expected)
    2) UserForm opens, i click OK button to close it.
    3) Now, none of my Buttons from 1-3 are selected, but my sheet is still shorted on one of my last active Buttons from 1-3.

    I want one of the 3 buttons to either maintain its bold text, or regain focus back on closing UserForm, dunno how to identify the last active CommandButton before clicking Commandbutton4. How do i do that?


    thanks

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Find what control has focus?

    Welcome to VBForums Mamu

    Rather than replying to somebody elses thread that is only vaguely related (and in this case 5 years old too), you should create your own thread.

    As you are working in the Excel VB Editor (which is VBA rather than VB), you should post in our Office Development forum.

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