Cesar Francisco

'This is trying to be a phone number formatting routine. Overall -- it is not working well. I would like to limit the entry to 10 digits only. Error reporting should be " " in txtboxPh2 when entry is alphabetic or even better, simply do not accept alphabetic entry.

Private Sub txtboxPh2_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtboxPh2.Leave
Dim sb As New System.Text.StringBuilder
Dim pos As Integer = txtboxPh2.SelectionStart
For Each c As Char In txtboxPh2.Text
If Char.IsNumber(c) Then
sb.Append(c)
End If
Next
txtboxPh2.Text = sb.ToString
txtboxPh2.SelectionStart = pos
Try
Dim numberPh As Integer
numberPh = Integer.Parse(txtboxPh2.Text)
If numberPh > 9999999999 Then
txtboxPh2.Text = " "
Else
'This part is the formatting part:
txtboxPh2.Text = Format(CInt(txtboxPh2.Text), "(###) ###-####")
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub



Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

Solitaire

First, set the MaxLength property of the textbox to 10. Then write code in the textbox KeyPress event to allow only numbers. The following code will allow editing keys:

If Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = Chr(Keys.Back) And Not e.KeyChar = Chr(Keys.Delete) And Not e.KeyChar = Chr(13) Or e.KeyChar = "." Then

e.Handled = True

End If

Other than that, you can use the MaskedTextBox control instead of the regular textbox. Find the Mask property and click on the ... next to it. A window will pop up with a list of items. Select the phone number from the list and you're good to go.






Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

Tall Dude

You can set this in the IDE properties or in the

load event:

Me.TextBox1.MaxLength = 10






Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

Tall Dude

Here is an alternate way to do some input checking.

(Also, I would not format the textbox. I would format it elsewhere

in output, or break it into three textboxes and add the '(' and '-' as

labels between the textboxes. In other words, don't put something

in a textbox that the user cannot put in the textbox.)

Public Class Form1

Dim allowed As String _

= "0123456789-" & ChrW(Keys.Back) & ChrW(Keys.Return)

Private Sub TextBox1_KeyPress(ByVal sender As System.Object, _

ByVal e As System.Windows.Forms.KeyPressEventArgs) _

Handles TextBox1.KeyPress

If allowed.IndexOf(e.KeyChar) = -1 Then

e.Handled = True

MsgBox("Number or dash only")

End If

End Sub

End Class






Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

Cesar Francisco

Thanks.

Key edititing doesn't bring me closer to the solution.

MaskedTextBox works well - absolutely. The problem with it: visible formatting clues () - .

:-(

With MaskedTextBox1

.Mask = "(###) ###-####"

.PromptChar = " "

End With

Could you help with this formatting string

It turns out that the CInt is a troublemaker.

txtboxPh2.Text = Format(CInt(txtboxPh2.Text), "(###) ###-####")






Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

Cesar Francisco

Thanks. Good ideas.

I tried to place the MaxLength into IDE properties columns Name/Type/Scope/Value. Is that what you are

suggesting It doesn't do anything there.

In the code below, limits the entry to 10 digit numbers in a txtboxPh2.TextChanged event.

Do I need the Leave event - How do I format the string otherwise

On further testing, it turns out that txtboxPh2.Text = Format(CInt(txtboxPh2.Text), "(###) ###-####") is not

working properly. The CInt is a problem.

Private Sub txtboxPh2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtboxPh2.TextChanged

txtboxPh2.MaxLength = 10

Dim sb As New System.Text.StringBuilder

Dim pos As Integer = txtboxPh2.SelectionStart

For Each c As Char In txtboxPh2.Text

If Char.IsNumber(c) Then

sb.Append(c)

End If

Next

txtboxPh2.Text = sb.ToString

txtboxPh2.SelectionStart = pos

End Sub

The three TextBoxes is also a good idea, but going down that road, the FormatTextBox would be the
simplest solution, but that would look messy in a DataEntry Form, specially if there will be no phone number

entered in the field.






Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

Solitaire

Why are you even attempting to convert a phone number into an integer A phone number is a string of characters and it should stay that way. It is not a value that you would ever want to process mathematically. It may also include the characters (, ), and -, which are non-numeric.

As suggested by Tall Dude, you can use a string to restrict the input. The following will allow only digits if you set the MaxLength to 10. It also allows the Enter key and Backspace key to work. You can include the (,), and - characters in the string and set the MaxLength to 13, but those characters can be placed anywhere, and even used more than once, so that's not a good solution.

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

Dim allow As String = "1234567890" & Chr(8) & Chr(13)

If allow.IndexOf(e.KeyChar) = -1 Then e.Handled = True

End Sub

To avoid any ambiguities, take Tall Dude's suggestion to use separate textboxes for each part of the phone number, restricting each to digits of a set MaxLength, and then setting the focus to the next textbox when the Enter key is pressed.

Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

Dim allow As String = "1234567890" & Chr(8)

If allow.IndexOf(e.KeyChar) = -1 Then e.Handled = True

If e.KeyChar = Chr(13) And TextBox2.Text.Length = 3 Then TextBox3.Focus()

End Sub






Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

Cesar Francisco

You are right. Merci beaucoup. Bonne idee.

Following your idea. Instead of a MaskedTextBox just drop in a string with the more

frequent area code "(519) "

into the Phonetextbox on Focus and erase it later if there are no further numbers entered.

Should this suffice as a hint

Why place brackets around the area code at all The phone book doesn't have it.

The version below ( a remake of the previous one) does a few things I like (am I obsessed ) :

1. Case: letters only are entered (they will be deleted)

2. Case: numbers and letters are entered (letters are ignored)

3. () and - are kept

4. Case: too many numbers (numbers stay) > (to do: make it replace by a " ")

5. Case: brackets and/or "-" + too many numbers (numbers stay) > (to do: make it replace by a " ")

6. DOB textbox data entry on this Form also accepts the format 31 1 75 (DD/MM/YY) so

the user is in a familiar territory by typing 416 234 2345 where the number clusters are

seprated by space only. (it is also a lot faster)

Dim sb As New System.Text.StringBuilder

Dim pos As String = txtboxPh2.SelectionStart

For Each c As Char In txtboxPh2.Text

If Char.IsNumber(c) Then

sb.Append(c)

End If

Next

txtboxPh2.Text = sb.ToString

txtboxPh2.SelectionStart = pos

If txtboxPh2.Text.Length = 10 Then

Dim AreaCode, NumberSet1, NumberSet2 As Int16

AreaCode = txtboxPh2.Text.Substring(0, 3)

NumberSet1 = txtboxPh2.Text.Substring(3, 3)

NumberSet2 = txtboxPh2.Text.Substring(6, 4)

txtboxPh2.Text = ("(" & AreaCode & ")" & NumberSet1 & "-" & NumberSet2)

End If

The problem with this set-up: how to limit the number of digits entered

It seems that is only possible with txtboxPh2.TextChanged event. What do you think






Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

Cesar Francisco

Tall Dude:

Three textboxes.

An interesting idea: a positive plus.

Allows for quick data entry (paired with "Enter" instead of "Tab" from textbox to textbox)

Blowback: this is a Data Entry Form. Should we add other two columns to the database

What do you think




Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

Cesar Francisco

Actually placing a " " or nothing into the textbox when the entry is more than 10 character solves the problem (I think).

Sure, it will not limit the entry to 10 characters but it will allow the user to reenter the phone number again

without causing further data entry delay by warning dialog boxes.

Thanks for your collaboration. Without you would have not been possible.






Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

BadButBit

hi,

instead of having the user jump from text box to text box and entering dashes, commas and what not.... you might want them to enter the phone number like they would dial a phone... nothing but numbers.

you leave them access to a txtBox where they enter what they want (you refuse anything that's not a number or a backspace) then as the string in the txtBox grows place the numbers in a label above the txtBox that looks more like a recognizable phone number than 10 digits in a row...

BadButBit





Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

Cesar Francisco

Again my failure of communication.

Let's see. This is a Data Entry Form with First, LastName,

DOB, Gender, Phone 1, Phone 2 and Notes.

I am jumping from textboxes to textboxes.

Not in the Phone textboxes itself.

If you look at the code, exactly what you recommend is

happening. First type numbers as you would dial

a phone, one after the other without space, brackets or dashes.

Formatting is done after the user leaves the Phone textbox.

I was just thinking out loud: what is the real purpose to put the

area code into brackets.

It is not particulary handsome and also not more helpful than a

dash.

The real solution would be like this (allow numbers, "-" and "x":

User enter three numbers, code writes "-", enter other three number , code writes "-"






Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

Cesar Francisco

Hello Solitaire,

I believe your code would be the best solution

( I would like to include "-" and also "x" for extension, no curly brackets).

Final phone number should look like 123-123-1234 and if is there any then "space" x123

I pasted your code into my Form but it doesn't seem to do anything.

No error in the code Window.

What did I miss

Private Sub txtboxPh2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles

txtboxPh2.KeyPress

Dim allow As String = "1234567890" & Chr(8) & Chr(13)

If allow.IndexOf(e.KeyChar) = -1 Then e.Handled = True

End Sub

Merci

Sorry, I have no idea what's happend-now is OK. I did add "-x ".






Re: Visual Basic Express Edition limit textbox entry to 10 "digits"

Cesar Francisco

I have one other (from Newbie to Newbie) solution to this problem:

It does

1. Limit the entry to Numbers only.

2. Format the entered numbers like:

a) (123)123-1234 / 123-123-1234 or

b) allow for extensions up to 4 digits like 123-111-4444 x1234

This is done by extending the TextBox Class and placing the code recommended by Solitaire in the code window.

If anybody interested in it, please post back.