Detect change of focus on form?

  • Thread starter Thread starter kkk
  • Start date Start date
K

kkk

I need to control something (visibility of toolbar buttons) when the cursor
moves between fields on a form (using VB .net). How do I detect that the
focus has moved?

Thanks,
Paul.
 
kkk said:
I need to control something (visibility of toolbar buttons) when the
cursor moves between fields on a form (using VB .net). How do I
detect that the focus has moved?


The control gets the Gotfocus/Lostfocus event.
 
Hi kkk,

Have a look at the lostfocus event that is in every control (form textbox
etc)

Cor
 
* "kkk said:
I need to control something (visibility of toolbar buttons) when the cursor
moves between fields on a form (using VB .net). How do I detect that the
focus has moved?

Loop through all controls recursively and add a common handler to their
'LostFocus' and 'GotFocus' events. There you can easily detect if
another control gets focus.
 
Hi kkk,

Herfried helped me to tell that I had a complete sample for that

I hope it helps

Cor

\\\
Dim last As String
Private Sub Form5_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
doset(Me)
End Sub
Private Sub doSet(ByVal parentCtr As Control)
Dim ctr As Control
For Each ctr In parentCtr.Controls
AddHandler ctr.LostFocus, AddressOf meLostFocus
AddHandler ctr.GotFocus, AddressOf meGotFocus
doSet(ctr)
Next
End Sub
Private Sub meLostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
last = DirectCast(sender, Control).Name
End Sub
Private Sub meGotFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, Control).Text = last
End Sub
///
 
Thanks for all the suggestions, which I see will work. But is there no way
to do this without having to put a handler on each control? Is there no
FORM-level event that I can handle?

THanks, Paul.
 
You can hook mouse events + keyboard ones on form and analyze where is
mouse, which key is pressed and find out controls underneath mouse / cursor.
But I think it will be much more complex and unnecessary, especially
considering that controls could be added / removed dynamically.

HTH
Alex
 
Back
Top