Active control

Dear Friends,

 i have to find out the last active control on the form how to do that any function is available to find out that. Like suppose i am entering values in Text1 after pressing tab the cursor goes to next control i.e. Text2
now in Text2.GotFocus() event i want to find out the last
active control how to do that. In this case Text1 is the last active control.

Plz help me.

Who is Participating?
OtanaConnect With a Mentor Commented:
you could put this in the lostfocus event of each control:

LastControl= Name-Of-Control

and make LastControl a public variable

But if you have many controls, it's quite a job to do so.
Maybe you could set a global variable to the name of the control which has just lost the focus, something like...

Option Explicit
Private lastfocus As String

Private Sub Text1_GotFocus()
    MsgBox lastfocus
End Sub
Private Sub Text2_GotFocus()
    MsgBox lastfocus
End Sub

Private Sub Text1_LostFocus()
    lastfocus = "Text1"
End Sub
Private Sub Text2_LostFocus()
    lastfocus = "Text2"
End Sub
It's one off those days!!!!
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

hijay23Author Commented:
No i don't want to do this i want any readymade function or API call.
I don't think you've got much choice here, but I might be mistaken.
TimCotteeHead of Software ServicesCommented:
There isn't a ready made function for this. Each control is autonomous within its container it is only responsible for events which occur whilst it is getting/has/losing focus you CANNOT infer the previous control from the current one in any way as the current control does not even know about the existence of any other controls unless it is bound to them in some fashion. You HAVE to follow one of the methods already suggested in some way in order to achieve your desired result.
why do you want such a thing, maybe there is an alternative for what you are trying to do.
You could try looping through each control on the container and pull out the one with the highest tabindex

For each ctl in form1.controls
  if ctl.tabindex > highindex then
     highindex = ctl.tabindex
     highname  =
  end iff
the highest but less than the current control tab index , but this wont work if you used a shortcut or mouse.
yes and if you use tab, why don't you just take the current tab index -1 ?
lol, right

always think simple.

but i think there can be a gab between, i mean you can have two controls only on a form , one with 0 tab index and the other with a 4 tab index ( i am not sure). :o(
hijay - what are you trying to do? sounds like a validation problem
(From MS Help)
ActiveControl Property

Returns the control that has thefocus. When a form is referenced, as in ChildForm.ActiveControl, ActiveControl specifies the control that would have the focus if the referenced form were active. Not available atdesign time; read-only atrun time.

However, I'm not sure how you would implement something like this in your case without creating an "endless" loop that checks for it.
After all, as soon as you click on something else, the control loses focus and is no longer active!
TimCotteeHead of Software ServicesCommented:
hijay23, please follow up on this question.
Hi hijay23,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Split points between: Otana and TimCottee

hijay23, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
DanRollins -- EE database cleanup volunteer
Per recommendation, force-accepted.

CS Moderator

TimCottee: points for you at
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.