Since your labels and text boxes essentially have the same name (it's only the prefix that's different) you can:
Bind all GetFocus
events to a single event handler.
Get the sender
's name (sender
is the control that raised the event), remove the txt
prefix and replace it with lbl
.
Look for a control by the new name (lbl...
).
If found, make it visible.
In code it'd look like this:
Private Sub TextBoxes_GotFocus(sender As Object, e As EventArgs) Handles txtFirstName.GotFocus, txtLastName.GotFocus, txtMiddleName.GotFocus, txtAddress.GotFocus, txtContact.GotFocus
Const NamePrefix As String = "txt"
Const NewPrefix As String = "lbl"
Dim ctrl As Control = TryCast(sender, Control)
If ctrl IsNot Nothing AndAlso ctrl.Name.StartsWith(NamePrefix) Then 'Check if the sender's name starts with our prefix.
Dim NewName As String = NewPrefix & ctrl.Name.Remove(0, NamePrefix.Length) 'Remove the old prefix and replace it with the new one.
Dim Controls As Control() = Me.Controls.Find(NewName, True) 'Look for the control of our new name.
If Controls.Length > 0 Then 'Did we find one?
Controls(0).Visible = True 'Make it visible.
End If
End If
End Sub
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…