01.Blogs :
travisowens  

Custom formatting of a TextBox

Custom formatting of a TextBox

I'm proud (or ashamed) to admit that I never got into Windows developing until Visual Studio.Net 2003 so I have the advantage of beginning my journey via the best tools ever made (for Windows).  This is also important to know because having no experience with COM components, I'm not familiar with any of the old tricks and when I approach a problem, I try to do it purely with the .Net tools & features and don't look into COM Components unless somebody suggests a solution from there.  Anyways, back to the point of today's mini lesson....

Part of the 'art of application developement' is to not just make something that works but to make something easy to use and smart, so the user can just get to the business of entering and analyzing data instead of worrying about dotting every i and crossing every t.

For this case I had a phone number field and I wanted to have built in formatting for it so that the textbox would display (xxx)xxx-xxxx  (where x is a space where a digit will go).  At first I thought I would have to create some wild regex checking while the user typed in values and that would have probably worked but after a little research I realized that there was an old COM component called MaskEdit which was a textbox that you can apply formatting to with restrictions on what kind of data can be entered.  I used the COM component but I was getting errors in my build because I had a method that would take all the TextBox's, and dupe their .Text to another set of TextBox'es if the user clicked a button.  Unfortunately this COM component didn't have a .Text property and I didn't want to code a second method (not an overload but a separate method) just to handle the duping of this special TextBox (aka MaskEdit) so a little more research unveiled that somebody had written their own MaskEdit control in .Net, which you can find here.

Just add the .Dll to your project and add the control to your ToolBox tab.  This control is much better than the old COM component and more efficient (coded in C# baby!) plus it conforms to .Net standards so .Text works (and probably many more features I haven't touched yet).

So to create my phone number box I only had to do the following things.

private MaskedEdit.MaskedEdit mskCallerPhone;

// The zeros represent a number only digit
mskCallerPhone.InputMask = "(000)000-0000";






That's it.... now we have a MaskEdit that looks like a TextBox and allows formatting and restrictions.  Then I only had to code an overload method for my CopyControl method to overload with a MaskEdit.MaskEdit instead of a TextBox.

.Net rules!

posted on Saturday, February 28, 2004 5:49 PM by travisowens


 
03.UPDATE CALENDAR :
<February 2004>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
29123456

05.MY LINKS :

07.Subscriptions :

Subscriptions


© Copyright 2005 Microsoft Corporation. All Rights Reserved.
Terms of Use | Privacy Statement | Code of Conduct | Hosted by MaximumASP for Microsoft
WHO-BAR