|
-
Oct 14th, 2005, 07:06 PM
#1
Thread Starter
Admodistrator
Find what control has focus?
Is there a way to find out which control has focus in vb? thanks
-
Oct 14th, 2005, 07:35 PM
#2
Re: Find what control has focus?
Use the ActiveControl property of the form, eg:
MsgBox Me.ActiveControl.Name
-
Oct 14th, 2005, 07:50 PM
#3
Thread Starter
Admodistrator
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?
-
Oct 14th, 2005, 07:54 PM
#4
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!)
-
Oct 14th, 2005, 08:23 PM
#5
Thread Starter
Admodistrator
Re: Find what control has focus?
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?
-
Oct 14th, 2005, 08:33 PM
#6
Frenzied Member
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.
-
Oct 14th, 2005, 08:35 PM
#7
Re: Find what control has focus?
-
Oct 14th, 2005, 08:35 PM
#8
Thread Starter
Admodistrator
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
-
Oct 14th, 2005, 08:39 PM
#9
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:
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
-
Apr 28th, 2010, 01:52 PM
#10
New Member
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
-
Apr 28th, 2010, 02:33 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|