By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,989 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,989 IT Pros & Developers. It's quick & easy.

Increasing Effciency of Custom Phone Number Textbox

P: n/a
I hate using the mask edit conrol so I created these two function to
format my phone numbers. Is there a way to consolidate these two
functions into one?

In this example say the textbox you want to format is named "txtTest"
I would format it in this fashion:
in the onchange event:

'Formats the text on entry
txtTest.text = FormatPhoneNumber(sender, txtTest.text)
in the keypress event for txtTest:

'Sends the object textbox and keypress to the ModType Function to
check for backspace...

ModType(sender, e)

Notes: I normaly set StopHypen as a Public Boolean....
Private Function FormatPhoneNumber(ByVal sender As System.Object,
ByVal phone As String) As String
Dim phoneFormater As String
Dim myTextbox As TextBox
myTextbox = CType(sender, TextBox)
If StopHypen = True Then
StopHypen = False
Return Mid((myTextbox.Text), 1, (Len(myTextbox.Text)))
myTextbox.SelectionStart = 7
Exit Function

End If
If dontRunOnLoad = False Then

If phone.Length = 1 And Mid(phone, 1) = "1" Then
phone = ""
phoneFormater = phone
myTextbox.SelectionStart = Len(myTextbox.Text)
myTextbox.SelectionLength = 0
Return (phoneFormater)

ElseIf phone.Length = 1 And Mid(phone, 1) <> "(" Then
phone = "(" & phone
phoneFormater = phone
myTextbox.SelectionStart = Len(myTextbox.Text)
myTextbox.SelectionLength = 0
Return (phoneFormater)

ElseIf phone.Length = 4 Then
phone = phone & ")"
phoneFormater = phone
myTextbox.SelectionStart = Len(myTextbox.Text)
myTextbox.SelectionLength = 0
Return (phoneFormater)
ElseIf phone.Length = 6 And Mid(phone, 6, 1) = "-" Then
phone = Mid(phone, 1, Len(phone) - 1)
phoneFormater = phone
Return (phoneFormater)

ElseIf phone.Length = 5 And Mid(phone, 5, 1) <> ")" Then

phone = Mid(phone, 1, 4) & ")" & Mid(phone, 5, 1)
' (603)1
phoneFormater = phone
Return (phoneFormater)

ElseIf phone.Length = 8 Then
phone = phone & "-"
phoneFormater = phone
myTextbox.SelectionStart = Len(myTextbox.Text)
myTextbox.SelectionLength = 0

Return (phoneFormater)

Else
phoneFormater = phone
myTextbox.SelectionStart = Len(myTextbox.Text)
myTextbox.SelectionLength = 0
Return (phoneFormater)

End If
Else

Return phone

End If

End Function
Private Function ModType(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
Dim MyTextBox As TextBox
MyTextBox = CType(sender, TextBox)
If e.KeyChar = ControlChars.Back = True Then
If Len(MyTextBox.Text) <> 9 And Len(MyTextBox.Text) <> 5
Then
MyTextBox.Text = Mid(MyTextBox.Text, 1,
(Len(MyTextBox.Text)))
ElseIf Len(MyTextBox.Text) = 5 Then
StopHypen = True
Else
StopHypen = True
MyTextBox.Text = Mid(MyTextBox.Text, 1, 8)
MyTextBox.SelectionStart = 8
End If

End If

End Function
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Peter,

Create your own textbox from inherriting the original and put all your
methods in that.

In your project you open add item usercontrol and change in top of that
inherits ...Control to Textbox, you are than almost halfway.

After Build you find it in your toolbox with the name you gave it.

I hope this helps?

Cor
Nov 21 '05 #2

P: n/a
Works great. LOL, I'm still learning. I started programming in
"turtle basic" on a vic 20.... This whole concept of objects is
fantastic....
Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.