C#"焦點(diǎn)事件"中的Validating處理方法 收藏
無(wú)意中接了個(gè)Window的項(xiàng)目,現(xiàn)總結(jié)Validating的處理方法,以供以后參考之用。焦點(diǎn)事件按下列順序發(fā)生:
Enter //進(jìn)入控件時(shí)發(fā)生
GotFocus //在控件接收焦點(diǎn)時(shí)發(fā)生
Leave //輸入焦點(diǎn)離開控件時(shí)發(fā)生
Validating //控件數(shù)據(jù)效驗(yàn)時(shí)發(fā)生
Validated //數(shù)據(jù)效驗(yàn)完成后發(fā)生
LostFocus //失去焦點(diǎn)時(shí)發(fā)生
如果 CausesValidation 屬性設(shè)置為 false,則將取消 Validating 和 Validated 事件。
注:GotFocus 和 LostFocus 事件是關(guān)聯(lián)于 WM_KILLFOCUS 和 WM_SETFOCUS Windows 消息的低級(jí)別焦點(diǎn)事件。應(yīng)對(duì)所有控件使用 Enter 和 Leave 事件。
如果在 Validating 事件委托中,CancelEventArgs 對(duì)象的 Cancel 屬性設(shè)置為 true,則正常情況下將在 Validating 事件之后發(fā)生的所有事件均被取消。
在操作中驗(yàn)證
要驗(yàn)證控件的內(nèi)容,可以編寫代碼來(lái)處理 Validating 事件。在事件處理程序中,測(cè)試特定的條件(例如上面的電話號(hào)碼)。驗(yàn)證是在處理時(shí)發(fā)生的一系列事件之一。
如果測(cè)試失敗,則 Validating 事件的 CancelEventArgs 的 Cancel 屬性將設(shè)置為 True。這將取消 Validating 事件,并導(dǎo)致焦點(diǎn)返回到控件(juky_huang注:這樣會(huì)出現(xiàn)一個(gè)死循環(huán),除非數(shù)據(jù)效驗(yàn)通過(guò),可以使用下面強(qiáng)制方法來(lái)關(guān)閉)。實(shí)際的結(jié)果是,除非數(shù)據(jù)有效,否則用戶將無(wú)法退出該控件。
關(guān)閉窗體和重寫驗(yàn)證
當(dāng)數(shù)據(jù)無(wú)效時(shí),維護(hù)焦點(diǎn)的控件的副作用是,使用關(guān)閉窗體的任何常規(guī)方法都將無(wú)法關(guān)閉父窗體:
單擊“關(guān)閉”框
通過(guò)右擊標(biāo)題欄顯示的“系統(tǒng)”菜單
以編程方式調(diào)用 Close 方法
不過(guò),在某些情況下,無(wú)論控件中的值是否有效,您都希望用戶可以關(guān)閉窗體。您可以重寫驗(yàn)證,并通過(guò)創(chuàng)建窗體的 Closing 事件的處理程序來(lái)關(guān)閉仍包含無(wú)效數(shù)據(jù)的窗體。在該事件中,將 Cancel 屬性設(shè)置為 False。這將強(qiáng)制關(guān)閉該窗體。
注意 如果使用此方法強(qiáng)制關(guān)閉窗體,控件中尚未保存的任何信息都將丟失。
注意 模式窗體在關(guān)閉時(shí)不會(huì)驗(yàn)證控件內(nèi)容。您仍可以使用控件驗(yàn)證將焦點(diǎn)鎖定到控件,但不必考慮關(guān)閉窗體的行為。
以下是事例代碼:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MSDNValidatingEx
{
/// <summary>
/// Form1 的摘要說(shuō)明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.ErrorProvider errorProvider1;
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗體設(shè)計(jì)器支持所必需的
//
//
// TODO: 在 InitializeComponent 調(diào)用后添加任何構(gòu)造函數(shù)代碼
//
InitializeComponent();
textBox1.Validating+=new CancelEventHandler(textBox1_Validating); //給textBox1添加效驗(yàn)函數(shù)
textBox1.Validated+=new EventHandler(textBox1_Validated);
}
private void textBox1_Validating(object sender,CancelEventArgs e)
{
string errorMsg;
if(!ValidEmailAddress(this.textBox1.Text,out errorMsg))
{
//如果效驗(yàn)沒有通過(guò)取消后繼事件,即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)
{
//首先判斷是否為空,然后判斷是否有@,.符號(hào)
if(emailAddress.Length==0)
{
errorMessage="e-mail address is required.";
return false;
}
//是否包含@
if(emailAddress.IndexOf("@")>-1)
{
//從@往后面開始搜索,找到.的位置,如果位置大于@的位置說(shuō)明格式正確
if(emailAddress.IndexOf(".",emailAddress.IndexOf("@"))>emailAddress.IndexOf("@"))
{
errorMessage="";
return true;
}
}
errorMessage="e-mail address must be valid e-mail address format.\n"+"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 窗體設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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>
/// 應(yīng)用程序的主入口點(diǎn)。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}