MaskedTextBox控件概述
MaskedTextBox控件的主要用途是限制用户在文本框中输入的字符类型、数量以及格式。通过设置预定义的掩码模式,MaskedTextBox控件可以使用户在输入时只能输入符合要求的字符,从而方便开发者对用户输入的数据进行验证、格式化等操作。
掩码元素
常用的掩码元素如下表所示:
掩码元素 | 说明 |
0 | 数字,必需。 此元素将接受 0 到 9 之间的任何个位数。 |
9 | 数字或空格,可选。 |
# | 数字或空格,可选。允许输入加号 (+) 和减号 (-)。 |
L | 字母,必需。 将输入限制为 ASCII 字母 a-z 和 A-Z。 |
? | 字母,可选。 将输入限制为 ASCII 字母 a-z 和 A-Z。 |
& | 字符,必需。 |
C | 字符,可选。 |
A | 字母数字,必需。 |
a | 字母数字,可选。 |
. | 小数点占位符。 |
, | 千位占位符。 |
: | 时间分隔符。 |
/ | 日期分隔符。 |
$ | 货币符号。 |
< | 向下移动。 将后面的所有字符转换为小写。 |
> | 向上移动。 将后面的所有字符转换为大写。 |
所有其他字符 | 文字。 所有非掩码元素将在 MaskedTextBox 中按原样显示。 文本在运行时始终占据掩码中的静态位置,用户不能移动或删除文本。 |
应用范例
需要在界面中输入以下信息:
1)用户名:只能是字母,不能是数值等其它字符;
2)密码:6位字母或数字组成;
3)电话:11位数字组成;
4)日期:按照****/**/**格式输入,只能为有效的日期格式;
5)时间:按照**:**:**格式输入,只能为有效的时间格式
完整实现代码:
using System;
using System.Windows.Forms;
namespace WinForm_MaskedTextBoxDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 设置 mtxtUserName 文本框的输入掩码。其中 "LL???" 表示用户必须输入包含至少2个字符,至多5个字符,且只能为字母。
mtxtUserName.Mask = "LL???";
// 当用户输入不合法字符时,触发 MaskInputRejected 事件,并执行 mtxtUserName_MaskInputRejected 方法。
mtxtUserName.MaskInputRejected += new MaskInputRejectedEventHandler(mtxtUserName_MaskInputRejected);
// 文本不包括提示字符或文字
mtxtPhoneNumber.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
// 当输入错误时,发出提示音。
mtxtPhoneNumber.BeepOnError = true;
// 设置电话号码的掩码,"000-0000-0000" 中,0 代表数字,- 代表分隔符。
mtxtPhoneNumber.Mask = "000-0000-0000";
// 文本包括提示字符和文字。
mtxtDate.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
// 当输入错误时,发出提示音。
mtxtDate.BeepOnError = true;
// 设置验证类型为 DateTime。
mtxtDate.ValidatingType = typeof(System.DateTime);
// 设置日期的掩码,只能按格式输入0-9的数字。
mtxtDate.Mask = "0000/00/00";
// 当类型验证完成时,执行 mtxtDate_TypeValidationCompleted 方法。
mtxtDate.TypeValidationCompleted += new TypeValidationEventHandler(mtxtDate_TypeValidationCompleted);
// 文本包括提示字符和文字。
mtxtTime.TextMaskFormat = MaskFormat.IncludePromptAndLiterals;
// 当输入错误时,发出提示音。
mtxtTime.BeepOnError = true;
// 设置验证类型为 DateTime。
mtxtTime.ValidatingType = typeof(System.DateTime);
// 设置时间的掩码,只能按格式输入0-9的数字。
mtxtTime.Mask = "00:00:00";
// 当类型验证完成时,执行 mtxtTime_TypeValidationCompleted 方法。
mtxtTime.TypeValidationCompleted += new TypeValidationEventHandler(mtxtTime_TypeValidationCompleted);
// 设置密码字符显示为 *。
mtxtPassword.PasswordChar = '*';
// 文本不包括提示字符或文字
mtxtPassword.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals;
// 当输入错误时,发出提示音。
mtxtPassword.BeepOnError = true;
// 设置密码的掩码,只能为6位数的字母或数字
mtxtPassword.Mask = "AAAAAA";
}
private void mtxtPhoneNumber_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
// 设置提示信息的标题
toolTip1.ToolTipTitle = "Invalid Input";
// 显示提示信息
toolTip1.Show("We're sorry, but only digit are allowed in PhoneNumber.", mtxtPhoneNumber, mtxtPhoneNumber.Location, 3000);
}
private void mtxtUserName_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
{
// 设置提示信息的标题
toolTip1.ToolTipTitle = "Invalid Input";
// 显示提示信息
toolTip1.Show("We're sorry, but only character are allowed in UserName.", mtxtUserName, mtxtUserName.Location, 3000);
}
private void mtxtTime_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
{
// 如果输入不是有效的时间类型,则执行以下代码块。
if (!e.IsValidInput)
{
// 设置提示信息的标题
toolTip1.ToolTipTitle = "Invalid Date Value";
// 显示提示信息
toolTip1.Show("We're sorry, but the value you entered is not a valid date. Please change the value.", mtxtTime, 3000);
// 取消此次事件的处理,防止无效的日期输入被接受。
// e.Cancel = true;
}
}
private void mtxtDate_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
{
// 如果输入不是有效的日期类型,则执行以下代码块。
if (!e.IsValidInput)
{
// 设置提示信息的标题
toolTip1.ToolTipTitle = "Invalid Date Value";
// 显示提示信息
toolTip1.Show("We're sorry, but the value you entered is not a valid date. Please change the value.", mtxtDate, 3000);
// 取消此次事件的处理,防止无效的日期输入被接受。
// e.Cancel = true;
}
}
}
}
运行效果: