Masking of Text Box

S

Suresh

Hi,

How to set Mask for TextBox component. For example, if a
TextBox is intended for Phone Number Data entry, how do we
enable it so that user can type using a mask like (999)>
999-9999?

Thanks
Suresh
 
C

Chris Capel

The only way I know of without getting a 3rd party component from somewhere
is to handle the TextBox.KeyDown event. Keep a string with everything
entered by the user so far, and when he presses a key, add the character to
the end of the string, or delete it. You could keep up with the current
position in the text box with another variable. Then, after you've updated
the input string, do something like this

string charSoFar;
textBox1.Text = "(" +
charSoFar.SubString(0, 3) +
") " +
charSoFar.SubString(3, 3) +
"-" +
charSoFar.SubString(6, 4);

Of course, you'd have to check to make sure there were that many characters
in the string. Does this help?

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top