Tuesday, December 15, 2009

C# Compact Framework: Numeric TextBox

I was recently in need of a numeric textbox for a small compact framework application I’m writing at home. The best I could find on the web was this MSDN article, but unfortunately it wasn’t as bullet proof as I’d like it to be, so I used the code as a base, and wrote my own.

Features:

  • Allows numeric only entry
  • DecimalPlaces property
    • Specify how many decimal places the number should have. 0 decimal places = integer numbers (no decimal separator allowed)
    • Defaults to 2 decimal places
  • AllowNegatives property
    • Specifies whether to allow negative number values or not
    • When the number is negative, the font changes color from black to red
    • defaults to not allow negative number entries

Sample application with code:

The code isn’t exactly pretty, but it works, and that’s all I was after.

Feel free to take it for a spin, and let me know if you can break it, so I can fix any loopholes.

1 comment:

Shreekanth Gaanji said...

That's a beautiful piece of code.
Thanks for saving my time :)