473,224 Members | 1,262 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Increasing Effciency of Custom Phone Number Textbox

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
2 1964
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Michael ALbanese | last post by:
I am developing a telephone directory for my company. I have Fist Name, Last Name, Department and Phone Number as fields. A user can search by entering data into any one, or combination of these...
100
by: jacob navia | last post by:
As everybody knows, C uses a zero delimited unbounded pointer for its representation of strings. This is extremely inefficient because at each query of the length of the string, the computer...
6
by: Poewood | last post by:
Is there a way to parse the contents of a textbox into a phone number format? Actually I would like to save the input as a number but display it as a phone number in the typical US format (000)...
2
by: Pham Nguyen | last post by:
Has anyone seen an example of a textbox server control that has built-in client-side validation? I'd like to build a server control that extends the System.Web.UI.WebControls.TextBox class to allow...
3
by: Hai Nguyen | last post by:
Hi All I have a textbox want to mask as phone box. How can I do it? Thanks
1
by: womblesjc | last post by:
I have a data bound Details View control in asp.net 2.0 that formats a phone number. The 'Default Mode' for the control is set to Edit. The phone number field is a template field and I can...
0
by: JeremyW | last post by:
I have a data bound Details View control in asp.net 2.0 that formats a phone number. The 'Default Mode' for the control is set to Edit. The phone number field is a template field and I can...
0
by: SenseiHitokiri | last post by:
I am trying to create a custom textbox that has an input mask to show the text as a phone number. I am very new to this language and trying to figure out how these pages relate. I thought I had...
0
by: mayankagarwal | last post by:
Over the last few days i have read a lot of forum about custom validator not working or not firing etc. I am stuck in a similar situation and of all the solutions i have come across they dont seem to...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.