Go homepage(回首页) Upload pictures (上传图片) Write articles (发文字帖)
The author:(作者)qqpublished in(发表于) 2014/7/11 9:17:23 C#中MaskedTextBox控件的用法以及实例
C#中MaskedTextBox控件的用法以及实例
MaskedTextBox控件1.功能MaskedTextBox控件使用掩码区分正确和不正确的用户输入。MaskedTextBox控件,如图1所示。图1 MaskedTextBox控件2.属性MaskedTextBox控件常用属性及说明如表1所示。表1 MaskedTextBox控件常用属性及说明下面详细介绍ValidatingType属性,该属性可以获取或设置用于验证用户输入数据的数据类型。语法:public Type ValidatingType { get; set; }属性值:表示在验证中使用的数据类型的Type。默认为空引用。示例ValidatingType属性把ValidatingType属性值设置为日期类型。程序主要代码如下:maskedTextBox1.ValidatingType = typeof(System.DateTime);3.方法(1)ClearUndo方法。该方法主要用于从文本框的撤消缓冲区中清除最近操作的信息。(2)ValidateText方法。该方法主要用来将用户输入的字符串转换为验证类别的一个实例。语法:public Object ValidateText ()返回值:如果转换成功,则为由ValidatingType属性指定的类型的Object;否则,将返回空引用,以指示转换失败。4.事件MaskedTextBox控件常用事件及说明如表2所示。表2 MaskedTextBox控件常用事件及说明下面详细介绍MaskInputRejected事件,该事件在用户的输入或者分配的字符与输入掩码的对应格式元素不匹配时发生。语法:public event MaskInputRejectedEventHandler MaskInputRejected说明:MaskInputRejected是MaskedTextBox类的默认事件。完整程序代码如下:★★★★★主程序文件完整程序代码★★★★★using System;using System.Collections.Generic;using System.Windows.Forms;namespace _8_18{static class Program{/// 本教程来自http://www.isstudy.com//// 应用程序的主入口点。/// [STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Application.Run(new frmMaskedTextBox());}}}★★★★★frmMaskedTextBox窗体设计文件完整程序代码网站源代码★★★★★using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace _8_18{public partial class frmMaskedTextBox : Form{public frmMaskedTextBox(){InitializeComponent();}Binding currentBinding, phoneBinding;DataSet employeesTable = new DataSet();SqlConnection sc;SqlDataAdapter dataConnect;private void frmMaskedTextBox_Load(object sender, EventArgs e){//maskedTextBox1.Mask = "00/00/0000";maskedTextBox1.ValidatingType = typeof(System.DateTime);}private void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e){toolTip1.ToolTipTitle = "日期";toolTip1.Show("你输入的类型不证确请输入一个日期", maskedTextBox1, 5000);}private void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e){}}}★★★★★frmMaskedTextBox窗体代码文件完整程序代码★★★★★namespace _8_18{partial class frmMaskedTextBox{/// 本教程来自http://www.isstudy.com//// 必需的设计器变量。/// private System.ComponentModel.IContainer components = null;/// /// 清理所有正在使用的资源。/// /// 如果应释放托管资源,为 true;否则为 false。protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码
赞