焦時間點事件中的Validating處理方法

來源:互聯網
上載者:User

本文轉載:http://tech.it168.com/oldarticle/2006-04-03/200604031055437.shtml

有時候,我們需要檢查使用者輸入到Windows表單中的資訊是否有效。例如,有一個電話號碼的TextBox控制項,需要檢查該控制項是否只包含適當的字元(數字、括弧和連字號等等)。通常,我們可使用Regex驗證使用者輸入的資料。

在瞭解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 表單設計器支援所必需的
   //
   //
   // TODO: 在 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.\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 表單設計器產生的程式碼
  /// <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());
  }
 }
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.