如何在没有 InvalidCastException 的情况下处理 TextBox 和 MaskTextBox 的事件?
How to handle an event for TextBox and MaskTextBox without InvalidCastException?
任何朋友请更正我的脚本,TextBox
变量工作完成但 MaskTextBox
在此 EnterEvent
.
中返回 error
我想将 event-handling
的一个函数用于 TextBox
和 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
事件处理程序无法工作,因为总是存在类型转换错误
正确的版本是
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
最好使用两个控件的共同祖先 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
我使用了@Jimi 的示例及其工作...
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
任何朋友请更正我的脚本,TextBox
变量工作完成但 MaskTextBox
在此 EnterEvent
.
error
我想将 event-handling
的一个函数用于 TextBox
和 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
事件处理程序无法工作,因为总是存在类型转换错误 正确的版本是
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
最好使用两个控件的共同祖先 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
我使用了@Jimi 的示例及其工作...
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