How can I get the previous control?

  • Thread starter Thread starter Wael Soliman
  • Start date Start date
W

Wael Soliman

I have many controls in the form I want to know which one lost the focus
(previous control)
For example:
In the form text1, text2, text3 and text4
The focus now in text1 and the user click by mouse on text3 the focus
now in text3 how can I know the last control is text1

Regards
 
* Wael Soliman said:
I have many controls in the form I want to know which one lost the focus
(previous control)
For example:
In the form text1, text2, text3 and text4
The focus now in text1 and the user click by mouse on text3 the focus
now in text3 how can I know the last control is text1

Something like 'Me.GetNextControl(Me.ActiveControl, False)'.
 
Thanks for your response
But the GetNextControl return next control in the tab order; false to
search backward I want to get the last focused control before focus the
current one
Regards
 
Hi Wael,

You did bring me on this idea, I saw no standard code, however this is very
generic for it.

I hope this helps?

Cor

\\\needs some controls which can have focus on a form
Dim last As String
Private Sub Form5_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
For Each ctr As Control In Me.Controls
AddHandler ctr.LostFocus, AddressOf meLostFocus
AddHandler ctr.GotFocus, AddressOf megotFocus
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
///
 
Wael,

Create a global variable to store a control in. Set the value of this
variable in the LostFocus event of the control.

HTH,

Geoff
 
* "Cor said:
\\\needs some controls which can have focus on a form
Dim last As String
Private Sub Form5_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
For Each ctr As Control In Me.Controls
AddHandler ctr.LostFocus, AddressOf meLostFocus
AddHandler ctr.GotFocus, AddressOf megotFocus
Next
End Sub

Nice idea. Notice that this will not work with nested controls. You
may want to loop though all controls recursively and add the handlers.
 
Herfried,
Nice idea. Notice that this will not work with nested controls. You
may want to loop though all controls recursively and add the handlers.

And that do you send to me, what did I say about recursive and me, here the
next good idea, I had also some in other newsgroups and a lot more, watch my
mail (about my new website) tomorrow.

\\\
Dim last As String
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.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
///

With this you can throw your hastable in my opinion in the disposing bin,

:-))

Cor
 
* "Cor said:
And that do you send to me, what did I say about recursive and me, here the
next good idea, I had also some in other newsgroups and a lot more, watch my
mail (about my new website) tomorrow.

:-)
 
Back
Top