Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a way of detecting which field on form has focus? 1

Status
Not open for further replies.

huggyboy

Programmer
Feb 7, 2003
108
GB

The title says it all really - some application or form property? cant find one

I'm sure is a programming way of doing it - on focus of every field set a public variable with its name to be picked up when i want to find the last one with focus kind of idea (will have to trap it some way when nothing has focus)

Thanks in anticipation
 
Interesting. I looked this up on Google, and this is the first result I found.


I like the last option looks to be the easiest. Create a recursive routine to test all controls. Something like:

Code:
Private FocusedControl As Control
...
    Sub TestFocus(ByRef c As Control)
        If c.Focused = True Then
            FocusedControl = c
        Else
            For Each c1 As Control In c.Controls
                TestFocus(c1)
            Next
        End If
    End Sub

Then call it with:

Code:
Me.FocusedControl = Nothing
Call TestFocus(Me)
If Not FocusedControl Is Nothing Then MessageBox.Show(FocusedControl.Name.ToString)
 
Excellent - thanks - it did cross my mind to search all the controls on the form and i came across the focused property just after i posted the problem.
Will look at the link later - will use the solution you recommended - it is more elegant than the programming solution.

Cheers
 
This is just a thought. But isn't Me.ActiveControl would do?
 
That seems to work as well - which poses the question what is the differnce between the active control and the control that has the focus?

Stars for you both (bit of a problem - havent worked out how to award them!!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top