SIGN UP MEMBER LOGIN:    
ARTICLE

Masked TextBox Control

Posted by Jibin Pan Articles | Windows Forms C# January 26, 2004
This Masked Edit TextBox intelligent user control enhances the function of TextBox control, which can mask the Date, IP Address, SSN, Phone number, digit, decimal and check the validation, automatically set delimit location.
Reader Level:
Download Files:
 

The TextBox control is the most used control in window program. It also cause a lot of problems either from QA or user, because the invalid data that were entered. Using masked control will solve these problems and save a lot of time for developer.

This masked intelligent user control enhances the function of TextBox control, which can mask the Date, IP Address, SSN, Phone number, digit, decimal and check the validation, automatically set delimit location.

The property Masked is set to None by default and the control works like a normal TextBox control.

If setting the property to DateOnly, the control is masked to Date format.

MaskedTextBox.jpg

Ex. When DateOnly is true 

When user enter    Display
12                         12/
124                       12/04/
13                         01/3
3                           03/
34                         03/04/
14                         01/04/
1/                          01/

Using the ErroProvider to handle the invalidate input: 

MaskedTextBoxImg002.jpg 

Creating Control:

1. Start the Visual Studio.NET Windows Forms designer.
2. Select a new C# project by clicking New from the File menu.
3. Click Windows control library template on the templates.
4. Set the Name MaskedTextBox 

Then open the maskedTextBox.cs to Change the base class to the System.Windows.Forms.TextBox.

Adding a Property:

public enum Mask
{
None, DateOnly, PhoneWithArea, IpAddress , SSN, Decimal, Digit };
private Mask m_mask;
public Mask Maked
{
get { return m_mask;}
set
{
m_mask =
value;
this.Text="";
}
}
Overwrite the OnKeyPress function.
this.KeyPress += new
KeyPressEventHandler(
this.OnKeyPress);
private void OnKeyPress(object sender, KeyPressEventArgs e)
{
MaskedTextBox sd = (MaskedTextBox) sender;
switch(m_mask)
{
case Mask.DateOnly:
sd.MaskDate(e);
break;
case Mask.PhoneWithArea:
sd.MaskPhoneSSN(e, 3, 2);
break;
case Mask.IpAddress:
sd.MaskIpAddr(e);
break;
case Mask.SSN:
break;
case Mask.Decimal:
sd.MaskDecimal(e);
break;
case Mask.Digit:
sd.MaskDigit(e);
break;
}
}
 

Unboxing the sender and using it to call these function are very important for multiple properties and more than one control are used in one form, otherwise different control may share same variable with other control, that will cause the unexpected results.

Double checke the masked format:

private void OnLeave(object sender, EventArgs e)
{
MaskedTextBox sd = (MaskedTextBox) sender;
Regex regStr;
switch(m_mask)
{
case Mask.DateOnly:
regStr =
new Regex(@"\d{2}/\d{2}/\d{4}");
if(!regStr.IsMatch(sd.Text))
errorProvider1.SetError(
this, "*");
break;
case Mask.PhoneWithArea:
regStr =
new Regex(@"\d{3}-\d{3}-\d{4}");
if(!regStr.IsMatch(sd.Text))
errorProvider1.SetError(
this,"**");
break;
case Mask.IpAddress:
short cnt=0;
int len = sd.Text.Length;
for(short i=0; i<len;i++)
if(sd.Text[i] == '.')
{
cnt++;
if(i+1 < len)
if(sd.Text[i+1] == '.')
{
errorProvider1.SetError(
this, "*");
break;
}
}
if(cnt < 3 || sd.Text[len-1] == '.')
errorProvider1.SetError(
this, "*");
break;
case Mask.SSN:
regStr =
new Regex(@"\d{3}-\d{2}-\d{4}");
if(!regStr.IsMatch(sd.Text))
errorProvider1.SetError(
this, "*");
break;
case Mask.Decimal:
break;
case Mask.Digit:
break;
}
}

Usage:

After creating the DLL program, you should add the component into ToolBox. By right clicking the mouse in the ToolBox, and then select Customize Toolbox to open a dialog. Select the tab .Net framework component, using browser to find your DLL, check it, then click Ok. The control should be in the ToolBox that is ready to use.

After creating the DLL program, you should add the component into ToolBox. By:

1. Right click the mouse in the ToolBox, and then select Customize Toolbox to open a dialog.

2. Select the tab .Net framework component,

3. Use the browser to find your DLL, check it, then click Ok.

The control should be in the ToolBox and it is ready to use.

That is all. I hope you enjoy it.

Login to add your contents and source code to this article
share this article :
post comment
 

Hi.. Jibin Pan h ru ? i checked u r code its gud to do that .. but i have one doudt in that in DateOnly format u have design in the format of MM/dd/yyyy style but i need dd/MM/yyyy style which is in indian style format so.. can u suggession in ur code format to change in dd/MM/yyyy format.. my email id srikanth9073@gmail.com

Posted by srikanth kumar Aug 19, 2011

thnks yaar gud coding

Posted by srikanth kumar Aug 11, 2011

How can I change the code to spanish format date: dd/mm/yyyy. thanks

Posted by Ariel Sep 10, 2008

Can we use in built Masked text box

Posted by gethzi akila Aug 06, 2007
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor