Auto-format decimal text as user enters numbers?

Up vote 1 down vote favorite share g+ share fb share tw.

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: 0.00 Tap the number 5: 0.05 Tap the number 8: 0.58 Then tap 3 - 2: 58.32 And if the user hits the delete key: 5.83 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?

C# wpf silverlight windows-phone-7 link|improve this question asked Dec 14 '11 at 21:34Tim Gabrhel201111 80% accept rate.

Yep, using the KeyDown event would be the way to do this. – Etch Dec 14 '11 at 21:37 1 Another important point is KeyDown doesn't fire when you paste something in. You will also need to listen to TextChanged.

– willmel Dec 14 '11 at 21:40 Thanks for that point. I didn't think of that. – Tim Gabrhel Dec 14 '11 at 22:06.

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.

I will give this a try and see what happens. I'm leaning towards the custom maskedtextbox I linked to in TysHTTP reply. – Tim Gabrhel Dec 14 '11 at 22:06 cool, let us know what works better?

– Tom Dec 14 '11 at 22:11.

I think you can accomplish just what you need.

Ow, I didn't see the tag Windows phone 7, i'm not sure whether you can used MaskedTextBox over there. – TysHTTP Dec 14 '11 at 21:56 Interesting. I didn't think of a MaskedTextBox...It doesn't appear that one exists, but I did find this post on the SL Forums: forums.silverlight.net/post/496643.aspx.

I'll have to give that custom control a shot. – Tim Gabrhel Dec 14 '11 at 22:01 Unfortunately, the MaskedTextProvider class isn't resolving in the System. ComponentModel assembly in WP7.

– Tim Gabrhel Dec 20 '11 at 2:01.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions