473,748 Members | 2,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 = FormatPhoneNumb er(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 FormatPhoneNumb er(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.Selec tionStart = 7
Exit Function

End If
If dontRunOnLoad = False Then

If phone.Length = 1 And Mid(phone, 1) = "1" Then
phone = ""
phoneFormater = phone
myTextbox.Selec tionStart = Len(myTextbox.T ext)
myTextbox.Selec tionLength = 0
Return (phoneFormater)

ElseIf phone.Length = 1 And Mid(phone, 1) <> "(" Then
phone = "(" & phone
phoneFormater = phone
myTextbox.Selec tionStart = Len(myTextbox.T ext)
myTextbox.Selec tionLength = 0
Return (phoneFormater)

ElseIf phone.Length = 4 Then
phone = phone & ")"
phoneFormater = phone
myTextbox.Selec tionStart = Len(myTextbox.T ext)
myTextbox.Selec tionLength = 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.Selec tionStart = Len(myTextbox.T ext)
myTextbox.Selec tionLength = 0

Return (phoneFormater)

Else
phoneFormater = phone
myTextbox.Selec tionStart = Len(myTextbox.T ext)
myTextbox.Selec tionLength = 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.KeyPressE ventArgs)
Dim MyTextBox As TextBox
MyTextBox = CType(sender, TextBox)
If e.KeyChar = ControlChars.Ba ck = True Then
If Len(MyTextBox.T ext) <> 9 And Len(MyTextBox.T ext) <> 5
Then
MyTextBox.Text = Mid(MyTextBox.T ext, 1,
(Len(MyTextBox. Text)))
ElseIf Len(MyTextBox.T ext) = 5 Then
StopHypen = True
Else
StopHypen = True
MyTextBox.Text = Mid(MyTextBox.T ext, 1, 8)
MyTextBox.Selec tionStart = 8
End If

End If

End Function
Nov 21 '05 #1
2 1988
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
4071
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 fields. I have been trying to write a custom validation control that will test each of the textboxes and reject the request if they are ALL blank.
100
3623
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 starts an unbounded memory scan searching for a zero that ends the string. A more efficient representation is: struct string {
6
29325
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) 000-0000. Thanx in advance, Poe
2
3921
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 javascript checks for things like valid e-mail addresses or phone numbers (without having to add a separate control for validation). One idea I did some work on was having the control implement the IValidator interface and basically recreating...
3
5216
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
3368
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 successfully call a function that formats the phone number when the control is filled. But when updating, the value being passed in to my format function is vbNull instead of the value in the phone number field of the control. How can I get the update...
0
1005
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 successfully call a function that formats the phone number when the control is filled. But when updating, the value being passed in to my format function is vbNull instead of the value in the phone number field of the control. How can I get the update...
0
1194
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 this set up but the control appears in an odd manner. It is supposed to just be an inherited textbox control but instead in the design window it shows up as black text like this in bold. It does not appear at all on the page and is completely...
0
1206
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 be working for me. I have a contact us form on my site (ASP.NET, AJAX, VB) and I need to validate the textbox input on most of the inputs i have use requiredfieldvalidator, expressionvalidators and they all work alright. What i also need to do is...
0
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9316
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6793
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.