C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Mask: You can find predefined masks for integers, phone numbers, dates, Social Security Numbers, time in various formats, and zip codes.
Note: This tutorial shows the MaskedTextBox with the Social Security Number format specified.
MaskInputRejected: Add this by double-clicking on the MaskedTextBox control. We show the RejectionHint and the Position of the error.
Example program for MaskedTextBox: C#
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void maskedTextBox1_MaskInputRejected(object sender,
MaskInputRejectedEventArgs e)
{
// Set the window title text
// ... to the MaskInputRejectedEventArgs information.
this.Text = "Error: " +
e.RejectionHint.ToString() +
"; position: " +
e.Position.ToString();
}
}
}
Then: We added the TypeValidationCompleted event handler by going to the event list in Visual Studio.
Tip: To get the validated result, please access the ReturnValue property from the TypeValidationEventArgs. We need to cast this object.
Info: The TypeValidationCompleted event handler is triggered when the form is dismissed.
Example program 2 for MaskedTextBox: C#
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void maskedTextBox1_TypeValidationCompleted(object sender,
TypeValidationEventArgs e)
{
// This event is raised when the enclosing window is closed.
// ... We show a MessageBox that details the DateTime.
DateTime value = (DateTime)e.ReturnValue;
MessageBox.Show("Validated: " + value.ToLongDateString());
}
}
}
AsciiOnly: The AsciiOnly property enforces that every character entered not be an accented or Unicode-only character.
Tip: This is useful when you require strict data input, as many database systems do not need non-ASCII characters.
UseSystemPasswordChar: Finally, you can use the system-defined password character for when your MaskedTextBox is a password box.
And: If your user types either of those characters, and the MaskedTextBox has one of those characters, this is considered valid input.