|
源代码网推荐
您可能经常需要检查用户输入到 Windows 窗体中的信息是否有效。例如,如果您有一个电话号码的 TextBox 控件,则可以检查该控件是否只包含适当的字符(数字、括号和连字符等等)。通常,可使用正则表达式验证用户输入的数据。
了解Validating之前,我们还需要了解焦点事件的顺序:
焦点事件按下列顺序发生:
- Enter //进入控件时发生
- GotFocus //在控件接收焦点时发生
- Leave //输入焦点离开控件时发生
- Validating //控件数据效验时发生
- Validated //数据效验完成后发生
- LostFocus //失去焦点时发生
如果 CausesValidation 属性设置为 false,则将取消 Validating 和 Validated 事件。
注:GotFocus 和 LostFocus 事件是关联于 WM_KILLFOCUS 和 WM_SETFOCUS Windows 消息的低级别焦点事件。应对所有控件使用 Enter 和 Leave 事件。
如果在 Validating 事件委托中,CancelEventArgs 对象的 Cancel 属性设置为 true,则正常情况下将在 Validating 事件之后发生的所有事件均被取消。
在操作中验证
要验证控件的内容,可以编写代码来处理 Validating 事件。在事件处理程序中,测试特定的条件(例如上面的电话号码)。验证是在处理时发生的一系列事件之一。
如果测试失败,则 Validating 事件的 CancelEventArgs 的 Cancel 属性将设置为 True。这将取消 Validating 事件,并导致焦点返回到控件(juky_huang注:这样会出现一个死循环,除非数据效验通过,可以使用下面强制方法来关闭)。实际的结果是,除非数据有效,否则用户将无法退出该控件。
关闭窗体和重写验证
当数据无效时,维护焦点的控件的副作用是,使用关闭窗体的任何常规方法都将无法关闭父窗体:
- 单击“关闭”框
- 通过右击标题栏显示的“系统”菜单
- 以编程方式调用 Close 方法
不过,在某些情况下,无论控件中的值是否有效,您都希望用户可以关闭窗体。您可以重写验证,并通过创建窗体的 Closing 事件的处理程序来关闭仍包含无效数据的窗体。在该事件中,将 Cancel 属性设置为 False。这将强制关闭该窗体。
注意 如果使用此方法强制关闭窗体,控件中尚未保存的任何信息都将丢失。
注意 模式窗体在关闭时不会验证控件内容。您仍可以使用控件验证将焦点锁定到控件,但不必考虑关闭窗体的行为。
以下是事例代码:
using System; 源代码网推荐using System.Drawing; 源代码网推荐using System.Collections; 源代码网推荐using System.ComponentModel; 源代码网推荐using System.Windows.Forms; 源代码网推荐using System.Data;
namespace MSDNValidatingEx 源代码网推荐{ 源代码网推荐 /// <summary> 源代码网推荐 /// Form1 的摘要说明。 源代码网推荐 /// </summary> 源代码网推荐 public class Form1 : System.Windows.Forms.Form 源代码网推荐 { 源代码网推荐 private System.Windows.Forms.TextBox textBox1; 源代码网推荐 private System.Windows.Forms.ErrorProvider errorProvider1; 源代码网推荐 /// <summary> 源代码网推荐 /// 必需的设计器变量。 源代码网推荐 /// </summary> 源代码网推荐 private System.ComponentModel.Container components = null;
public Form1() 源代码网推荐 { 源代码网推荐 // 源代码网推荐 // Windows 窗体设计器支持所必需的 源代码网推荐 // 源代码网推荐 // 源代码网推荐 // TOD 在 InitializeComponent 调用后添加任何构造函数代码 源代码网推荐 // 源代码网推荐 InitializeComponent(); 源代码网推荐 textBox1.Validating+=new CancelEventHandler(textBox1_Validating); //给textBox1添加效验函数 源代码网推荐 textBox1.Validated+=new EventHandler(textBox1_Validated); 源代码网推荐 }
private void textBox1_Validating(object sender,CancelEventArgs e) 源代码网推荐 { 源代码网推荐 string errorMsg; 源代码网推荐 if(!ValidEmailAddress(this.textBox1.Text,out errorMsg)) 源代码网推荐 { 源代码网推荐 //如果效验没有通过取消后继事件,即Validated,LostFocus 源代码网推荐 e.Cancel=true; 源代码网推荐 this.textBox1.Select(0,this.textBox1.Text.Length); 源代码网推荐 this.errorProvider1.SetError(this.textBox1,errorMsg); 源代码网推荐 } 源代码网推荐 }
private void textBox1_Validated(object sender,EventArgs e) 源代码网推荐 { 源代码网推荐 errorProvider1.SetError(this.textBox1,""); 源代码网推荐 }
public bool ValidEmailAddress(string emailAddress,out string errorMessage) 源代码网推荐 { 源代码网推荐 //首先判断是否为空,然后判断是否有@,.符号 源代码网推荐 if(emailAddress.Length==0) 源代码网推荐 { 源代码网推荐 errorMessage="e-mail address is required."; 源代码网推荐 return false; 源代码网推荐 } 源代码网推荐 //是否包含@ 源代码网推荐 if(emailAddress.IndexOf("@")>-1) 源代码网推荐 { 源代码网推荐 //从@往后面开始搜索,找到.的位置,如果位置大于@的位置说明格式正确 源代码网推荐 if(emailAddress.IndexOf(".",emailAddress.IndexOf("@"))>emailAddress.IndexOf("@")) 源代码网推荐 { 源代码网推荐 errorMessage=""; 源代码网推荐 return true; 源代码网推荐 } 源代码网推荐 }
errorMessage="e-mail address must be valid e-mail address format.
"+"For example "someone@example.com""; 源代码网推荐 return false; 源代码网推荐 }
/// <summary> 源代码网推荐 /// 清理所有正在使用的资源。 源代码网推荐 /// </summary> 源代码网推荐 protected override void Dispose( bool disposing ) 源代码网推荐 { 源代码网推荐 if( disposing ) 源代码网推荐 { 源代码网推荐 if (components != null) 源代码网推荐 { 源代码网推荐 components.Dispose(); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 base.Dispose( disposing ); 源代码网推荐 }
#region Windows 窗体设计器生成的代码 源代码网推荐 /// <summary> 源代码网推荐 /// 设计器支持所需的方法 - 不要使用代码编辑器修改 源代码网推荐 /// 此方法的内容。 源代码网推荐 /// </summary> 源代码网推荐 private void InitializeComponent() 源代码网推荐 { 源代码网推荐 this.textBox1 = new System.Windows.Forms.TextBox(); 源代码网推荐 this.errorProvider1 = new System.Windows.Forms.ErrorProvider(); 源代码网推荐 this.SuspendLayout(); 源代码网推荐 // 源代码网推荐 // textBox1 源代码网推荐 // 源代码网推荐 this.textBox1.Location = new System.Drawing.Point(72, 88); 源代码网推荐 this.textBox1.Name = "textBox1"; 源代码网推荐 this.textBox1.TabIndex = 0; 源代码网推荐 this.textBox1.Text = ""; 源代码网推荐 // 源代码网推荐 // errorProvider1 源代码网推荐 // 源代码网推荐 this.errorProvider1.ContainerControl = this; 源代码网推荐 // 源代码网推荐 // Form1 源代码网推荐 // 源代码网推荐 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); 源代码网推荐 this.ClientSize = new System.Drawing.Size(292, 273); 源代码网推荐 this.Controls.Add(this.textBox1); 源代码网推荐 this.Name = "Form1"; 源代码网推荐 this.Text = "Form1"; 源代码网推荐 this.ResumeLayout(false);
} 源代码网推荐 #endregion
/// <summary> 源代码网推荐 /// 应用程序的主入口点。 源代码网推荐 /// </summary> 源代码网推荐 [STAThread] 源代码网推荐 static void Main() 源代码网推荐 { 源代码网推荐 Application.Run(new Form1()); 源代码网推荐 } 源代码网推荐 } 源代码网推荐} 源代码网推荐
源代码网供稿. |