|
|
Title | Keep track of the last control to have the focus |
Keywords | last control, focus |
Categories | Software Engineering, Controls |
|
|
This is handy, for example, if the program does something that moves the input focus and wants to restore focus the the last control the user selected.
The program uses form variables CurrentControl and LastControl to keep track. It updates them in every control's GotFocus event handler.
|
|
Private CurrentControl As Control
Private LastControl As Control
' Return a control's name.
Private Function ControlName(ByVal ctl As Control) As String
Dim ctl_name As String
Dim ctl_index As Integer
If ctl Is Nothing Then
ControlName = "-"
Else
ctl_name = ctl.Name
On Error Resume Next
ctl_index = ctl.Index
If Err.Number = 0 Then
' No error. The control has an index.
ctl_name = ctl_name & _
"(" & Format$(ctl_index) & ")"
End If
ControlName = ctl_name
End If
End Function
Private Sub ShowControls()
lblCurrentControl.Caption = ControlName(CurrentControl)
lblLastControl.Caption = ControlName(LastControl)
End Sub
Private Sub Text1_GotFocus()
Set LastControl = CurrentControl
Set CurrentControl = Text1
ShowControls
End Sub
Private Sub Text2_GotFocus()
Set LastControl = CurrentControl
Set CurrentControl = Text2
ShowControls
End Sub
Private Sub Text3_GotFocus(Index As Integer)
Set LastControl = CurrentControl
Set CurrentControl = Text3(Index)
ShowControls
End Sub
|
|
You could extend this method, possibly using a collection, to keep track of more previously used controls.
|
|
|
|
|
|