How to get the last active control

  • Thread starter Thread starter amnonevo
  • Start date Start date
Hi,

Probably there is a much better way, when not, than you can maybe try this
sample I created.

\\\\Needs by instance a form with two textoboxes and surely one button and
pasting this code in
Private PreviousControl As Control
Private Sub Form1_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.Leave, AddressOf meLeave
doSet(ctr)
Next
End Sub
Private Sub meLeave(ByVal sender As Object, _
ByVal e As System.EventArgs)
PreviousControl = DirectCast(sender, Control)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show(PreviousControl.Text)
End Sub
///

I hope this helps,

Cor
 
Cor,

Thanks for the code. It does help. Just one comment: it seems to me that
the call to doSet(ctr) in the For Each loop in Sub doSet is redundant.
Don't you think so?
 
Amnon,
Just one comment: it seems to me that the call
to doSet(ctr) in the For Each loop in Sub doSet is redundant.
Don't you think so?

No normally it is not.
It is a recursive method.
It gets as well all the controls that are in another control.

However if you are sure that you have placed your controls only direct on
your form. Than it is redundant.

I hope this gives the idea?

Cor
 
Back
Top