how to filter out non-digit chars when user writes text to System.Windows.Forms.TextBox

  • Thread starter Thread starter Ivan
  • Start date Start date
I

Ivan

Hi,
how to filter out non-digit chars when user writes text to System.Windows.Forms.TextBox?

Thanks,
Iavmn
 
Hi,
how to filter out non-digit chars when user writes text to
System.Windows.Forms.TextBox?

Thanks,
Iavmn

Subscribe to the TextBox.KeyPress event. Check the character in the event
arguments. If it's a non-digit, set e.Handled=true; and that should prevent
the character from being allowed. You might want to do a beep or something
as well.

Pete
 
Pete said:
Subscribe to the TextBox.KeyPress event. Check the character in the event
arguments. If it's a non-digit, set e.Handled=true; and that should prevent
the character from being allowed. You might want to do a beep or something
as well.

Pete

that worked. thanks
Ivan
 
Note that this technique only prevents users from manually typing the
non-digit characters in the textbox - they can still paste the non-digit
characters from the clipboard. You might want to also implement an event
handler for the TextChanged property and replace all non-digit characters
there.
 

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

Back
Top