-1

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
IvanH
  • 5,039
  • 14
  • 60
  • 81
  • 1
    Use [GetType](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/operators/gettype-operator) to check the type of `sender`. – Filburt Sep 24 '18 at 21:35
  • You don't need to know the type if you're using properties derived from a common class. `Control`, in this case. Just cast `sender` to `Control`: `CType(sender, Control).BackColor = Color.Yellow CType(sender, Control).ForeColor = Color.Black`. It applies to all the Controls that "share". the event. – Jimi Sep 24 '18 at 21:51
  • There is no reason for a single event handler since there is no shared/common code! – Ňɏssa Pøngjǣrdenlarp Sep 24 '18 at 21:58
  • @New Contributor It's not really clear what you're saying here. There is common code. The code is supposed to change the `BackColor/ForeColor` of any of the Controls that subscribe the event. What are you referring to?. – Jimi Sep 24 '18 at 22:08
  • @NewContributor is correct that you should not use a common event handler if you're not going to do the same thing for each object that raises it. If you were to use a `If...Else` like that then the common event handler would be pointless. In this case though, it's the `If...Else` that's pointless because you want to do the exact same thing regardless, so just do the exact same thing. The reason that the properties have the same name is they are the same properties, inherited from `Control`, so you should use that common type as @Jimi suggested, or use the common base class `TextBoxBase`. – jmcilhinney Sep 24 '18 at 23:56

2 Answers2

1

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
IvanH
  • 5,039
  • 14
  • 60
  • 81
1

i am used Example of @Jimi and its Working...

Private Sub MaskedTextBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrgNameTextBox.Enter, OrgNameTextBox.Leave        
    CType(sender, Control).BackColor = Color.Yellow
    CType(sender, Control).ForeColor = Color.Black
End Sub