Private Sub txtNumbers_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNumber1.GotFocus, txtNumber2.GotFocus, txtNumber3.GotFocus
Dim TextBox As TextBox = CType(sender, TextBox)
Dim strPhone As String = TextBox.Text
Dim mblnPhoneError As Boolean = False
Dim strPhonePiece1 As String
Dim strPhonePiece2 As String
Dim strPhonePiece3 As String
If mblnPhoneError = False Then
If Len(strPhone) > 0 And Mid(strPhone, 1, 1) = "(" And TextBox.ReadOnly = False Then
strPhonePiece1 = Mid(strPhone, 2, 3)
strPhonePiece2 = Mid(strPhone, 7, 3)
strPhonePiece3 = Mid(strPhone, 11, 4)
strPhone = strPhonePiece1 & strPhonePiece2 & strPhonePiece3
TextBox.Text = strPhone
TextBox.SelectAll()
End If
Else
mblnPhoneError = False
End If
End Sub
Private Sub txtNumbers_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNumber1.LostFocus, txtNumber2.LostFocus, txtNumber3.LostFocus
Dim TextBox As TextBox = CType(sender, TextBox)
Dim strPhone As String = TextBox.Text
Dim strphonePiece1 As String
Dim strphonePiece2 As String
Dim strphonePiece3 As String
Dim blnNoValidType1 As Boolean = False
Dim blnNoValidType2 As Boolean = False
Dim blnNoValidType3 As Boolean = False
Dim blnNoValidType4 As Boolean = False
If TextBox.ReadOnly = False Then
If Len(TextBox.Text) <> 0 Then
If Len(TextBox.Text) = 12 And Mid(strPhone, 4, 1) = "-" And Mid(strPhone, 8, 1) = "-" Then
strphonePiece1 = Mid(strPhone, 1, 3)
strphonePiece2 = Mid(strPhone, 5, 3)
strphonePiece3 = Mid(strPhone, 9, 4)
strPhone = "(" & strphonePiece1 & ") " & strphonePiece2 & "-" & strphonePiece3
TextBox.Text = strPhone
Else
blnNoValidType1 = True
End If
If Len(TextBox.Text) = 10 And IsNumeric(Mid(strPhone, 1, 10)) = True Then
strphonePiece1 = Mid(strPhone, 1, 3)
strphonePiece2 = Mid(strPhone, 4, 3)
strphonePiece3 = Mid(strPhone, 7, 4)
strPhone = "(" & strphonePiece1 & ") " & strphonePiece2 & "-" & strphonePiece3
TextBox.Text = strPhone
Else
blnNoValidType2 = True
End If
If Len(TextBox.Text) = 14 And Mid(strPhone, 1, 1) = "(" Then
Else
blnNoValidType3 = True
End If
If Len(TextBox.Text) = 13 And Mid(strPhone, 1, 1) = "(" And IsNumeric(Mid(strPhone, 6, 3)) = True And IsNumeric(Mid(strPhone, 10, 4)) = True Then
strphonePiece1 = Mid(strPhone, 2, 3)
strphonePiece2 = Mid(strPhone, 6, 3)
strphonePiece3 = Mid(strPhone, 10, 4)
strPhone = "(" & strphonePiece1 & ") " & strphonePiece2 & "-" & strphonePiece3
TextBox.Text = strPhone
Else
blnNoValidType4 = True
End If
If blnNoValidType1 = True And blnNoValidType2 = True And blnNoValidType3 = True And blnNoValidType4 = True Then
ttPhoneError.Active = True
TextBox.ForeColor = Color.Red
Select Case TextBox.Name
Case "txtNumber1" : mblnSafeToSave1 = False
Case "txtNumber2" : mblnSafeToSave2 = False
Case "txtNumber3" : mblnSafeToSave3 = False
End Select
Else
ttPhoneError.Active = False
TextBox.ForeColor = mclrtxtForeColor
Select Case TextBox.Name
Case "txtNumber1" : mblnSafeToSave1 = True
Case "txtNumber2" : mblnSafeToSave2 = True
Case "txtNumber3" : mblnSafeToSave3 = True
End Select
End If
Else
TextBox.ForeColor = mclrtxtForeColor
End If
End If
End Sub