有4个选项可以为我的应用程序输入电话号码
- 0000-0000
- 00000-0000
- (00)0000-0000
- (00)00000-0000
我正在使用“ Maskedtextbox”进行处理,并且正在使用以下代码:
Private Sub TB_Telefone_11_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TB_Telefone_11.KeyPress
' Format when lenght = 8 > 0000-0000
If TB_Telefone_11.Text.Length = 8 Then
TB_Telefone_11.Mask = "0000-0000"
TB_Telefone_11.SelectionStart = Len(TB_Telefone_11.Text)
' Formart when lenght = 9 > 00000-0000
ElseIf TB_Telefone_11.Text.Length = 9 Then
TB_Telefone_11.Mask = "00000-0000"
TB_Telefone_11.SelectionStart = Len(TB_Telefone_11.Text)
' Format when lenght = 10 > (00)0000-0000
ElseIf TB_Telefone_11.Text.Length = 10 Then
TB_Telefone_11.Mask = "(00)0000-0000"
TB_Telefone_11.SelectionStart = Len(TB_Telefone_11.Text)
' Format when lenght = 11 > (00)00000-0000
ElseIf TB_Telefone_11.Text.Length = 11 Then
TB_Telefone_11.Mask = "(00)00000-0000"
TB_Telefone_11.SelectionStart = Len(TB_Telefone_11.Text)
End If
End Sub
问题是当文本为空时,返回10的大小,这是因为当文本为空时,返回:
' Size of 10 with spaces and etc.
( ) -
当您插入数字时,它可以正常工作,但是当您开始擦除时,它会停留在text.lenght = 10并保留该掩码。
答案 0 :(得分:0)
请改用TextChanged
事件。另外,添加文本长度小于8的情况以将掩码重置为空字符串。
Private Sub TB_Telefone_11_TextChanged(sender As Object, e As EventArgs) Handles TB_Telefone_11.TextChanged
' Format when lenght = 8 > 0000-0000
If TB_Telefone_11.Text.Length = 8 Then
TB_Telefone_11.Mask = "0000-0000"
TB_Telefone_11.SelectionStart = Len(TB_Telefone_11.Text)
' Formart when lenght = 9 > 00000-0000
ElseIf TB_Telefone_11.Text.Length = 9 Then
TB_Telefone_11.Mask = "00000-0000"
TB_Telefone_11.SelectionStart = Len(TB_Telefone_11.Text)
' Format when lenght = 10 > (00)0000-0000
ElseIf TB_Telefone_11.Text.Length = 10 Then
TB_Telefone_11.Mask = "(00)0000-0000"
TB_Telefone_11.SelectionStart = Len(TB_Telefone_11.Text)
' Format when lenght = 11 > (00)00000-0000
ElseIf TB_Telefone_11.Text.Length = 11 Then
TB_Telefone_11.Mask = "(00)00000-0000"
TB_Telefone_11.SelectionStart = Len(TB_Telefone_11.Text)
ElseIf TB_Telefone_11.Text.Length < 8 Then
TB_Telefone_11.Mask = ""
TB_Telefone_11.SelectionStart = Len(TB_Telefone_11.Text)
End If
End Sub