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

Formatting databound textbox as a phone number in VB.NET

P: 4
I have a databound textbox that I would like formatted as a phone number ((###) ###-####), instead of being displayed as 10 numbers.

I'm using this on a databound label, which works fine. The same doesn't work on a textbox, though.

phoneNumber = CDbl(lblPhoneNumber.Text)

lblPhoneNumber.Text = phoneNumber.ToString("(###) ###-####")


Anyone know of something that will work with a textbox?
Mar 27 '12 #1
Share this Question
Share on Google+
2 Replies


PsychoCoder
Expert Mod 100+
P: 465
Change this:
Expand|Select|Wrap|Line Numbers
  1. phoneNumber.ToString("(###) ###-####")
to this:
Expand|Select|Wrap|Line Numbers
  1. phoneNumber.ToString("{0:(###)-###-####}")
EDIT: You could also use this to accomplish your task:
Expand|Select|Wrap|Line Numbers
  1. double phone = double.Parse(txtPhone.Text);
  2. lblPhoneNumber.Text =  String.Format("{0:###-###-####}", phone);
Mar 28 '12 #2

P: 4
Okay, got it.

So, was getting some out of bounds for the record when looking at other records (have a form set up where you can view a record at a time).

So, under the text changed event, I used:

Expand|Select|Wrap|Line Numbers
  1. If IsNumeric(txtPhoneNumber.Text) Then
  2.       phoneNumber = CDbl(txtPhoneNumber.Text.Trim)
  3. End If
  4.  
Then under button click events (when moving through records), added:

Expand|Select|Wrap|Line Numbers
  1. txtPhoneNumber.Text = phoneNumber
to set it back to a number, so I don't get an error.

And then under form load event and binding source position changed event, I have this:

Expand|Select|Wrap|Line Numbers
  1. If Not phoneNumber = 0 Then
  2.             txtPhoneNumber.Text = String.Format("{0:(###) ###-####}", phoneNumber)
  3. End If
  4.  
And it's workin. It'd be nice if it could be a one liner though, like with a label, but whichever lol.


Thanks for your help.
Mar 29 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.