Click here to Skip to main content
14,696,609 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hello , I want to make the textbox's formats as phone number , while I type for example 55555555 it will will appear as 55 555 555

What I have tried:

I tried in the text_changed event to make the format as string.format ( "{0:#, ## ### ###}", convert.toint (textbox.text) )
but it didn't work , the cursor keeps changing its place while I type
Posted
Updated 6-Jul-20 23:57pm
Comments
Gerry Schmitz 6-Jul-20 17:11pm
   
Show your code; you're possibly not "handling" the event properly.

You can move the cursor back where it "belongs" by reading the SelectionStart property to find out where the user had it, and writing it to set it's new position, but it's messy, and you'll need to think carefully about where it should be each time. You will also have to handle "undoing" the format before you convert it to a number (which mobile phone numbers aren't: they generally start with a leading zero which will be removed by a conversion and may start with a "+" character and a country code)

You can find FormattedTextBox controls (of varying utility and quality) with Google, but personally I wouldn't bother: if you want to display a formatted mobile number, then I would format it once when the user leaves the control, and "unformat" it when he enters again.
That's a lot easier than "on the fly" formatting, both for you and the user typing.
   
v2
Comments
Luc Pattyn 6-Jul-20 17:53pm
   
I agree, when I encounter a "smart" input field, I run away full speed.

I don't want someone/something changing what I type, and certainly not while I'm typing.

And how about I try to correct a typo and hit backspace/delete? Do I have to take into account spaces or other delimiters I never entered? It is all horrible.

Format AFTER input not during.
Member 14190053 6-Jul-20 20:02pm
   
and what if the textbox I want to format is the last one before I add the data to the database ? there won't be leave event if I press enter , is there a way to invoke leave event when I add data even if I don't actually leave the textbox ?
OriginalGriff 7-Jul-20 2:01am
   
So use the Validating event instead.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900