Tim Gabrhel
Tim Gabrhel

Reputation: 446

auto-format decimal text as user enters numbers

I think I am trying to do something like described here, but in a different language.

I have a Textbox that allows for a decimal based number to be entered. I want to eliminate the need for hitting the decimal key. I have the input scope on the textbox set to 'Number', so the user has numbers and a decimal to hit. Here's an example of what I'd like to see:

Initial display:

Tap the number 5:

Tap the number 8:

Then tap 3 - 2:

And if the user hits the delete key:

I'm not sure how to go about this. I'm thinking that the default value of the textbox is 0.00, and in the OnKeyDown event, take their number, and set the value appropriately?

Upvotes: 1

Views: 846

Answers (2)

Tys
Tys

Reputation: 3610

Why not use a MaskedTextBox, with a Mask like this: 00.00 and set RightToLeft to Yes? I think you can accomplish just what you need.

Upvotes: 1

Tom Cerul
Tom Cerul

Reputation: 1943

sounds like you're almost there.

 If the key is a digit, 
  float work = float.tryparse(textbox.value )
  work *= 10; // move decimal
  work += key / 100; // move new digit 2 decimals right
  textbox.value = work.ToString();

For backspace, I wouldn't bother with the math, just substring off the last character.

Upvotes: 1

Related Questions