Gold
Gold

Reputation: 62424

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

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.

Upvotes: 2

Views: 296

Answers (2)

RvdK
RvdK

Reputation: 19790

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.

Upvotes: 0

lc.
lc.

Reputation: 116458

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.

Upvotes: 4

Related Questions