tags:

views:

51

answers:

2

How do I force a user to type only two digits after the decimal point?

For example:

100 - good
100.1 - good
10.21 - good
10.123 - bad
21.1234 - bad

I need it at entry time - in C# and Windows CE.

+4  A: 

If it's a WinForms TextBox, you can trap the KeyPressed event and test validity/squelch input then. However this won't directly prevent a user from pasting in an invalid value, so you will still need to do proper validation.

lc
A: 

You can use a maskedtextbox and let .NET do the work for you. MSDN But I don't know if this is available on the WindowsCE platform. If not: use the KeyPressed event of a normal textbox and check the input, if the input is bad. Put the key to handled so it will be not entered in the textbox.

PoweRoy
The link you provided states it only suites the full framework.
Shaihi
Not available in the CF.
ctacke
@Shaihi: thanks I just see it in the MSDN page. @ctack: Therefore I provided an alternative in my answer.
PoweRoy