Is there a way to find out which control has focus in vb? thanks :wave:
Printable View
Is there a way to find out which control has focus in vb? thanks :wave:
Use the ActiveControl property of the form, eg:
MsgBox Me.ActiveControl.Name
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?
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!)
Instead, i tried to use something like:
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?VB Code:
For Each Ctl In Me
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.
That's a very good idea!
ive never used withevents, although i have read about it. I will do some more research and report back once i figure this out :wave:
Here is some code you can use. Create a form with a Text1 control array, a listbox and a command button.
VB Code:
Option Explicit Dim mctlLast As Control Private Sub Command1_Click() MsgBox "The last control was " & mctlLast.Name On Error Resume Next If mctlLast.Index >= 0 Then MsgBox "And it's index is " & mctlLast.Index End If End Sub Private Sub Command1_LostFocus() Set mctlLast = Command1 End Sub Private Sub List1_LostFocus() Set mctlLast = List1 End Sub Private Sub Text1_LostFocus(Index As Integer) Set mctlLast = Text1(Index) End Sub
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
Welcome to VBForums Mamu :wave:
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.