Vb.net Text Box limit

kr80

Member
Joined
May 31, 2005
Messages
9
Programming Experience
1-3
Is there a way to accept telephone numbers in a Text Box ? I have used validations to accept only a particular format. But is it possible to get it like Access does.
For example the user needs to type in only the numbers and the ( ) - - are entered automatically.
Thanks,
Karl
 
i did this type of thing myself for a form that holds 3 phone numbers per record (per person really) i had three textboxes with 1 event for all three (the handles clause is nice):

VB.NET:
Private mblnCtrlKey As Boolean

	Private Sub txtNumbers_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNumber1.KeyDown, txtNumber2.KeyDown, txtNumber3.KeyDown
		If e.Control = True Then
			mblnCtrlKey = True
		Else
			mblnCtrlKey = False
		End If
	End Sub

	Private Sub txtNumbers_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumber1.KeyPress, txtNumber2.KeyPress, txtNumber3.KeyPress
		Dim TextBox As TextBox = CType(sender, TextBox)
		TextBox.SelectedText = ""
		If TextBox.Text.Length = 0 Then TextBox.ForeColor = mclrtxtForeColor
		If mblnCtrlKey = False And Asc(e.KeyChar) <> Keys.V And Asc(e.KeyChar) <> Keys.C Then
			KeyPressNumber(sender, e)
		End If
	End Sub

	Friend Sub KeyPressNumber(ByRef Sender As Object, ByRef e As System.Windows.Forms.KeyPressEventArgs)
		Dim TextBox As TextBox = CType(Sender, TextBox)
		Dim intTextLength As Integer
		Dim strLastChar As String
		Dim intX As Integer
		TextBox.SelectedText = ""
		intTextLength = TextBox.TextLength
		If intTextLength >= TextBox.MaxLength And Asc(e.KeyChar) <> Keys.Back And Asc(e.KeyChar) <> Keys.Enter And Asc(e.KeyChar) <> Keys.Escape Then
			e.Handled = True
		Else
			If Asc(e.KeyChar) <> Keys.Back And Asc(e.KeyChar) <> Keys.Enter And Asc(e.KeyChar) <> Keys.Escape Then
				Select Case CStr(e.KeyChar)
					Case "0"
					    TextBox.SelectedText = "0"
					Case "1"
					    TextBox.SelectedText = "1"
					Case "2"
					    TextBox.SelectedText = "2"
					Case "3"
					    TextBox.SelectedText = "3"
					Case "4"
					    TextBox.SelectedText = "4"
					Case "5"
					    TextBox.SelectedText = "5"
					Case "6"
					    TextBox.SelectedText = "6"
					Case "7"
					    TextBox.SelectedText = "7"
					Case "8"
					    TextBox.SelectedText = "8"
					Case "9"
					    TextBox.SelectedText = "9"
					Case "-"
					    TextBox.SelectedText = "-"
					Case "("
					    TextBox.SelectedText = "("
					Case ")"
					    TextBox.SelectedText = ")"
					Case " "
					    TextBox.SelectedText = " "
				End Select
				e.Handled = True
			End If
		End If
	End Sub

that handles the numbers only being typed into the textbox's with allowing ctrl+c/x and ctrl+v capabilities as well so that may seem kind of confusing

and now the actual formatting of the numbers that were entered into the textbox's (adding the parenthesis and dash so it looks like a phone number and not just 9 digits):
VB.NET:
	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
 
not a problem, i could have cleaned up the code some, but i'm glad you got it working with your application :)
 
Back
Top