Does anyone know how to do a Regex for the following scenario's?

  • Thread starter Thread starter Top Gun
  • Start date Start date
T

Top Gun

I have a textbox that displays money amounts as 999999999.99. The user would
like the keyer to either type in a decimal point or, if one is NOT supplied,
imply one. Furthermore, when the textbox is populated from the database
using a money datatype, it is four decimals to the right, which must be
formatted to two.

So, as far as keying, if a user (as an extreme) types in the following, they
should diplay as:

1 display as 0.01

..1 display as 1.10

100 display as 1.00

1.00 display as 1.00

1.0000 display as 1.00

etc.

I'm sure this can be done using Regex, but it seems a little tricky.
 
why in the world would you use Regex for this?

a little bit of logic in the KeyPress event should be all you need to
interpret the keys correctly.

Why not create your own control, derived from the textbox control, that will
handle this for you. Add a 'Value' field to the list of properties...

a) when the 'Value' field is assigned, make sure it is a number, and simply
format the to the correct number of decimal points. Lookup from database is
now taken care of.
b) when Value is assigned a 0, display 0.00 in the empty field. When you
create a new record, you now have a way to initialize the display.
c) When the user gets to the field, you take control of the KeyPress
functionality. With every keypress, you display the correct visible
representation.

display then user presses
0.00 1
0.01 0
0.10 4
1.04 3
10.43 decimal
1043.00 4
1043.40 6
1043.46 7
no change 6
no change

Now, when your code asks for the value of the field: you return 1043.46 as a
currency value.

That shouldn't be too hard to write.

--- Nick
 
Back
Top