473,406 Members | 2,847 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,406 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 1970
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.