Depending on the type of control that is GAINING or LOSING control there are many events within the event cycles.
One place you can read more is here
http://www.thevbprogrammer.com/VBNET_08/08-00_VB2K_ControlBasics.htm[
^]
Another place is here...
http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol.activecontrol(v=vs.90).aspx[
^]
The second example is much better as it uses the
ACTIVECONTROL method to return the currently active control.
Taking a step back an looking at the larger picture allows us modify the sample in the second link...
such as for example...
Public Class Form1
Private mPreviousControl As Control = Nothing
Private mActiveControl As Control = Nothing
Public Sub MyActiveControl(sender As System.Object, e As System.EventArgs)
If (sender Is Nothing) = False Then
mPreviousControl = mActiveControl
mActiveControl = sender
ListBox1.Items.Add("Active control is: " & mActiveControl.Name & Chr(13) & "Previous control was: " & mPreviousControl.Name)
End If
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
MyActiveControl(sender, e)
End Sub
Private Sub Button1_GotFocus(sender As System.Object, e As System.EventArgs) Handles Button1.GotFocus
MyActiveControl(sender, e)
End Sub
Private Sub CheckBox1_GotFocus(sender As System.Object, e As System.EventArgs) Handles CheckBox1.GotFocus
MyActiveControl(sender, e)
End Sub
Private Sub ComboBox1_GotFocus(sender As System.Object, e As System.EventArgs) Handles ComboBox1.GotFocus
MyActiveControl(sender, e)
End Sub
Private Sub ListBox1_GotFocus(sender As System.Object, e As System.EventArgs) Handles ListBox1.GotFocus
MyActiveControl(sender, e)
End Sub
Private Sub Label1_GotFocus(sender As System.Object, e As System.EventArgs) Handles Label1.GotFocus
MyActiveControl(sender, e)
End Sub
Private Sub RadioButton1_GotFocus(sender As System.Object, e As System.EventArgs) Handles RadioButton1.GotFocus
MyActiveControl(sender, e)
End Sub
Private Sub TextBox1_GotFocus(sender As System.Object, e As System.EventArgs) Handles TextBox1.GotFocus
MyActiveControl(sender, e)
End Sub
Private Sub PictureBox1_GotFocus(sender As System.Object, e As System.EventArgs) Handles PictureBox1.GotFocus
MyActiveControl(sender, e)
End Sub
End Class
Using the sample above you can simply drop one each of the following controls
Button
Checkbox
Listbox
Combobox
Label
Radiobutton
Textbox
Picturebox
and then as you tab through the controls on the form the information is added to the listbox to show you which control is CURRENTLY active and which control was PREVIOSULY active.
The downside to this is off course that you MUST call the MyActiveSub procedure Immediately in your controls so that its gaining focus can be catalogued.
Ideally though, you really should process the event actions within the procedure for the relevant control as that is the preferred text-book method and standard procedure, after all, thats why the methods exist!
Additionally
Off course you dont need ANY of the above and can simply interogate the the Forms ACTIVECONTROL property to findout which control on the form is currently active such as....
dim some_variable as string
dim some_control as control
some_variable = me.activecontrol.name
some_control = me.activecontrol