Masking of Text Box

  • Thread starter Thread starter Suresh
  • Start date Start date
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
 
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
 
Back
Top