Any buddy please correct my script, TextBox
variable working done but MaskTextBox
Returning error
in this EnterEvent
.
I want to use one Function of event-handling
for TextBox
and MaskTextBox
Private Sub TextBox_Enter(sender As Object, e As EventArgs) Handles OrgNameTextBox.Enter, AddressTextBox.Enter, ContactNumMaskedTextBox.Enter
Dim Tb As TextBox = CType(sender, TextBox)
Dim Mtb As MaskedTextBox = CType(sender, MaskedTextBox)
If Type = MASKTEXTBOX Then
MTb.BackColor = Color.Yellow
MTb.ForeColor = Color.Black
ElseIf Type = TextBox Then
Tb.BackColor = Color.Yellow
Tb.ForeColor = Color.Black
End If
End Sub
The event handler cannot work because there is always type conversion error The correct version is
Private Sub TextBox_Enter(sender As Object, e As EventArgs) Handles OrgNameTextBox.Enter, AddressTextBox.Enter, ContactNumMaskedTextBox.Enter
If TypeOf sender Is MaskedTextBox Then
Dim Mtb As MaskedTextBox = CType(sender, MaskedTextBox)
Mtb.BackColor = Color.Yellow
Mtb.ForeColor = Color.Black
ElseIf TypeOf sender Is TextBox Then
Dim Tb As TextBox = CType(sender, TextBox)
Tb.BackColor = Color.Yellow
Tb.ForeColor = Color.Black
End If
End Sub
Better is using common ancestor of both controls TextBoxBase
Private Sub TextBox_Enter(sender As Object, e As EventArgs) Handles OrgNameTextBox.Enter, AddressTextBox.Enter, ContactNumMaskedTextBox.Enter
Dim bt As TextBoxBase = TryCast(sender, TextBoxBase)
If bt IsNot Nothing Then
bt.BackColor = Color.Yellow
bt.ForeColor = Color.Black
End If
End Sub