Wednesday, March 21, 2007

Why MaskedTextBox is Bad

In Visual Studio 2005 Microsoft included a Mask control called MaskedTextBox. This control acted like a text box control, but allowed at design time or runtime the ability to add a mask to help the user input proper data.

This conception this is a great idea. The implementation however is not very good. There are a few problems.

The control does not know how to shift characters. An example would be if the mask was $##,###.00 and we had the amount of 100 saved in the data base and we tried to fill the control with that amount the result displayed would be $10,0__.__ where _ is the prompt character. The same problem occurs when the user tried to enter the data. Lets use a format for time like ##:##. When the form loads, the control will look something like this __:__ again using _ as the prompt character. the cursor will be on the left most space so if a user is not careful, the time of 9:30 can be enter like 93:0_. If the user corrects the mistake and saves the value 9:30 the control will load 09:30.

Since most of things that require mask are numbers and things of that nature, it makes this control pretty mush useless.

I found a few posts on the web that discuss this problem and how they solved it.
http://www.adduxis.com/blogs/blogs/sven/archive/2006/06/21/18.aspx
http://www.dotnetheaven.com/Uploadfile/mgold/MaskedCurrencyTextBox02252006005553AM/MaskedCurrencyTextBox.aspx

1 comment:

Anonymous said...

I can't believe that this functionality isn't available natively in .NET. Surely 90+ percent of the time a masked textbox would be used to enter dates, times, numbers, etc. As implemented, MaskedTextBox controls are useless. Sheesh! We had this capability 20 years ago and more in dBASE and similar products. Progress, eh?