Jump to content

vb.net function question

cab11150904

I am trying to make a function that will take raw phone number input such as 5551234567 and turn it into (555)123-4567.  I need to be able to enter the number into one text box and then a function should take and format it and then pass it to another textbox.  I have included the instructions below but for some reason I can't find a way to program a function to do it and then pass it to the other box.  I am headed to work and I'm basically out of time to work on it for today so I will check back in 12 or so hours to see if anyone has been able to help.  As the title says I'm programming in vb.Net  Thanks

 

1)             Write the code for the “Format Phone Number” button click event.  The subroutine should get the text from txtPhoneNumber and pass it to a function called FormatPhoneNumber.  The FormatPhoneNumber function should add parentheses around the area code and insert dashes before the prefix and trunk.  The FormatPhoneNumber should return the formatted phone number.  The “Format Phone Number” click event should update the text in txtFormattedPhoneNumber with that string.

Link to comment
Share on other sites

Link to post
Share on other sites

Exactly what are you having the issue with? And do you have any code so far? I can think of many ways to do it, but the most efficient would likely be out of the scope of this. I would suggest writing a subroutine that accepts the number as a string, makes sure it's a valid phone number (like test the length, I guess, for the scope of this), and add the parentheses and hyphon

Link to comment
Share on other sites

Link to post
Share on other sites

I've only worked with VBA with Access, so i'm not 100% sure on this, but this is how I would process the String

Function FormatPhoneNumber(MyVal PhoneNum As String)FormatPhoneNumber = "(" & Left(PhoneNum,3) & ")" & Mid(PhoneNum,4,3) & "-" & Right(PhoneNum,4)End Function
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×