Creating a masked input

Learning Resources
 

Creating a masked input


The following code sets masks programmatically, and achieves what we have demonstrated in the above sample using Input Mask dialog. The following code example initializes the MaskedTextBox to accept a Zip Code, and uses both the MaskInputRejected event to alert the user to invalid input.
 

private void CreateMaskedTextBox()
{
mtb = new MaskedTextBox();
mtb.Location = new Point(30, 30);
mtb.Mask = "00000-9999"; // For Zip Code
mtb.MaskInputRejected += new MaskInputRejectedEventHandler(mtb_MaskInputRejected);
Controls.Add(mtb);
}
 
void mtb_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
 
{
// Add a namespace System.Media;
// System sounds a beep every time the user attempts to input
// an invalid character
SystemSounds.Beep.Play();
}
 

 For Support