MaskedTextBox Madness
Over the last couple days, most of my time has been devoted to the MaskedTextBox control, and one very annoying bug. The issues revolves around the way the control deals with currency. When provided with a mask such as ‘$999,999,999‘ – you’d expect it to exercise some degree of intelligence. That expectation turned out to be quite wrong.
If you type the number ’50′ in a box with that mask, you’ll end up with ‘$500,000,000′ – for a financial application such as the one I’m working on, that could lead to a major issue. The only way to get the proper value is to set the cursor two places from the right. So if the user doesn’t pay attention, it’ll be simple to enter a value that is completely wrong.
To add insult to injury, when the control receives focus, it has a habit of setting focus to the end of the mask, where the user can not type due to the length restriction. So the user has to move the cursor to the desired location before they can start to type, and if thy just start typing, the control happily ignores them.
After testing a couple possible solutions (this and this) it soon became clear that the MaskedTextBox is too limited in features to be useful for currency. That simple.
My solution to this was to drop the Masked control and create a new control inherited from TextBox that supports a few extra features such as:
- Numeric only input
- Minimum Value
- Maximum Value
- Format String support
With less than 30 lines of code, the basic control was in place. I wish I could include a proper answer for this issue to make the MaskedTextBox useful, but after loosing two days, I can safely say it’s too limited to be of real value for even slightly complex uses.
One Response to MaskedTextBox Madness
Welcome!
I am a software developer, currently located in Eastern Tennessee. While my primary focus is creating software on Microsoft's .NET stack, I also write about other technologies and development in general.Search
Articles
- January 2012
- October 2011
- July 2011
- June 2011
- May 2011
- April 2011
- March 2011
- February 2011
- January 2011
- December 2010
- August 2010
- July 2010
- June 2010
- April 2010
- February 2010
- December 2009
- October 2009
- July 2009
- June 2009
- December 2008
- November 2008
- October 2007
- August 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006
- February 2006










Adam: Please check the FAQ on windowsforms.net (Here) The solution is the use the FormatString property on the DataBinding object of the Text property. HTH, SA.