在C#中,MaskedTextBox控件为日期,电话号码等表单上的用户输入提供验证过程。换句话说,它用于提供可区分正确和不正确用户输入的掩码。 MaskedTextBox类用于表示Windows蒙版的文本框,还提供不同类型的属性,方法和事件。它在System.Windows.Forms命名空间下定义。
此类是TextBox控件的增强版本,它支持用于接收或拒绝用户输入的声明性语法,并且当此控件在运行时显示时,它将掩码表示为提示字符和可选字面量字符的序列。在C#中,您可以使用两种不同的方法在Windows窗体中创建MaskedTextBox:
1.设计时:这是创建MaskedTextBox的最简单方法,如以下步骤所示:
- 第1步:创建一个Windows窗体,如下图所示:
Visual Studio->文件->新建->项目-> WindowsFormApp - 步骤2:接下来,将MaskedTextBox控件从工具箱拖放到窗体。
- 步骤3:拖放之后,您将转到MaskedTextBox控件的属性,以根据需要修改MaskedTextBox。
输出:
2.运行时:比上述方法有些棘手。在此方法中,可以借助MaskedTextBox类提供的语法以编程方式创建MaskedTextBox控件。以下步骤显示如何动态设置创建MaskedTextBox:
- 步骤1:使用MaskedTextBox类提供的MaskedTextBox()构造函数创建MaskedTextBox控件。
// Creating a MaskedTextBox control MaskedTextBox mbox = new MaskedTextBox();
- 步骤2:创建MaskedTextBox控件后,设置MaskedTextBox类提供的MaskedTextBox控件的属性。
// Setting the properties // of MaskedTextBox mbox.Location = new Point(374, 137); mbox.Mask = "000000000"; mbox.Size = new Size(176, 20); mbox.Name = "MyBox"; mbox.Font = new Font("Bauhaus 93", 18);
- 步骤3:最后,使用以下语句将此MaskedTextBox控件添加到表单中:
// Adding MaskedTextBox // control on the form this.Controls.Add(mbox);
例子:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp36 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Creating and setting the // properties of the Label Label l1 = new Label(); l1.Location = new Point(413, 98); l1.Size = new Size(176, 20); l1.Text = " Example"; l1.Font = new Font("Bauhaus 93", 12); // Adding label on the form this.Controls.Add(l1); // Creating and setting the // properties of the Label Label l2 = new Label(); l2.Location = new Point(242, 135); l2.Size = new Size(126, 20); l2.Text = "Phone number:"; l2.Font = new Font("Bauhaus 93", 12); // Adding label on the form this.Controls.Add(l2); // Creating and setting the // properties of the MaskedTextBox MaskedTextBox mbox = new MaskedTextBox(); mbox.Location = new Point(374, 137); mbox.Mask = "000000000"; mbox.Size = new Size(176, 20); mbox.Name = "MyBox"; mbox.Font = new Font("Bauhaus 93", 18); // Adding MaskedTextBox // control on the form this.Controls.Add(mbox); } } }
输出:
建设者
Constructor | Description |
---|---|
MaskedTextBox() | This Constructors is used to initialize a new instance of the MaskedTextBox class. |
MaskedTextBox(MaskedTextProvider) | This Constructors is used to initialize a new instance of the MaskedTextBox class using the specified custom mask language provider. |
MaskedTextBox(String) | This Constructors is used to initialize a new instance of the MaskedTextBox class using the specified input mask. |
特性
Property | Description |
---|---|
AsciiOnly | Gets or sets a value indicating whether the MaskedTextBox control accepts characters outside of the ASCII character set. |
AutoSize | This property is used to get or set a value that indicates whether the control resizes based on its contents. |
BackColor | This property is used to get or set the background color for the control. |
BorderStyle | This property indicates the border style for the control. |
Font | This property is used to get or set the font of the text displayed by the control. |
ForeColor | This property is used to get or set the foreground color of the control. |
Height | This property is used to get or set the height of the control. |
Location | This property is used to get or set the coordinates of the upper-left corner of the MaskedTextBox control relative to the upper-left corner of its form. |
Name | This property is used to get or set the name of the control. |
TabStop | This property is used to get or set a value that shows whether the user can press the TAB key to provide the focus to the NumericUpDown. |
Size | This property is used to get or set the height and width of the control. |
Text | This property is used to get or set the text to be displayed in the RichTextBox control. |
Visible | This property is used to get or set a value indicating whether the control and all its child controls are displayed. |
Width | This property is used to get or set the width of the control. |
Multiline | This property is used to get or set a value indicating whether this is a multiline MaskedTextBox control. |
TextAlign | This property is used to get or set how text is aligned in a masked text box control. |
TextMaskFormat | This property is used to get or set a value that determines whether literals and prompt characters are included in the formatted string. |
SelectedText | This property is used to get or set the current selection in the MaskedTextBox control. |
PromptChar | This property is used to get or set the character used to represent the absence of user input in MaskedTextBox. |
ReadOnly | This property is used to get or set a value indicating whether text in the text box is read-only. |
MaxLength | This property is used to get or set the maximum number of characters the user can type or paste into the text box control. This property is not supported by MaskedTextBox. |
Lines | This property is used to get or set the lines of text in multiline configurations. This property is not supported by MaskedTextBox. |