2012-07-31 49 views
0

我正在創建Windows窗體,它將驗證richtextbox中的文本並根據給定的要求獲取錯誤。我現在唯一的問題是,每次richtextbox改變驗證過程時,我的意思是每一次按鍵。我如何設置一個計時器,以便用戶每次在richtextbox中編輯一些文本時,都會等待幾秒鐘來驗證文本,而不僅僅是每次按鍵。需要明確的是,這裏是我的代碼:在Windows窗體中插入計時器

private void richTextBox1_TextChanged(object sender, EventArgs e) 
{ 
    listView1.Items.Clear(); 

    //Requirement.cs is a class where all the functions for formatting is placed. 

    Requirement req_obj = new Requirement(); 
    req_obj.check_casing(richTextBox1, listView1, errors); 
    req_obj.check_punctuation(richTextBox1, listView1, errors); 
    req_obj.check_spacing(richTextBox1, listView1, errors); 
    req_obj.check_abbreviation(richTextBox1, listView1, errors); 
    req_obj.check_others(richTextBox1, listView1, errors); 

    label2.Text = "Warning(s) found:" + req_obj.error_counter; 

發生了什麼事是,每次在用戶更改r.textbox的東西,它會自動驗證每一個按鍵。 我想要的是每次用戶編輯文本時設置計時器,用戶編輯文本後,計時器計數3,然後執行特定的過程。 我該怎麼做?感謝

+0

爲什麼不使用windows窗體的驗證功能?然後,您可以在驗證事件中實現您的驗證代碼。 – 2012-07-31 08:08:41

+0

我已經試過了,但它與textChanged事件一樣,每按一次鍵它都會自動驗證。 – neo 2012-07-31 08:10:31

+0

使用計時器也沒有意義,只是隨機對用戶輸入進行剔除。使用驗證事件,以便在用戶選中另一個控件時觸發。或者使用確定按鈕。 – 2012-07-31 09:02:15

回答

0

使用System.Windows.Forms.Timer

(請確保您使用的Forms.Timer是你想要的節拍處理程序被稱爲UI線程)

private void richTextBox1_TextChanged(object sender, EventArgs e) 
{ 
    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); 
    timer.Interval = 3000; 
    timer.Tick += ValidationTimer_Tick; 
    timer.Start(); 

    // ... 
} 

private void ValidationTimer_Tick(object sender, EventArgs e) 
{ 
    System.Windows.Forms.Timer timer = sender as System.Windows.Forms.Timer; 
    if(timer != null) 
    { 
     timer.Stop(); 
     timer.Dispose(); 

     // do validation 
    } 
} 
0

這裏的回答我的問題,在如果有人會爲我的問題尋找答案。

我設定的計時週期爲3000,這樣做:

private void richTextBox1_TextChanged(object sender, EventArgs e) 
     { 
      timer1.Enabled = true; 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 

      errors = new List<ListviewError>(); 
      listView1.Items.Clear(); 

      Requirement req_obj = new Requirement(); 
      req_obj.check_casing(richTextBox1, listView1, errors); 
      req_obj.check_punctuation(richTextBox1, listView1, errors); 
      req_obj.check_spacing(richTextBox1, listView1, errors); 
      req_obj.check_abbreviation(richTextBox1, listView1, errors); 
      req_obj.check_others(richTextBox1, listView1, errors); 

      label2.Text = "Warning(s) found:" + req_obj.error_counter; 

      timer1.Enabled = false; 
     } 
0

如果你能夠使用的無功框架(Rx)的,那麼你可以很容易地做這樣的時刻碼。

試試這個:

public Form1() 
    { 
     InitializeComponent(); 

     Observable 
      .FromEventPattern(
       h => richTextBox1.TextChanged += h, 
       h => richTextBox1.TextChanged -= h) 
      .Select(_ => Observable.Timer(TimeSpan.FromSeconds(3.0))) 
      .Switch() 
      .ObserveOn(this) 
      .Subscribe(_ => 
      { 
       /* Your validation code goes here */ 
      }); 
    } 

的代碼自動處理豐富的文本框中的文本改變時,等待3秒,除非另一個文本發生變化時,如果它做它再次開始等待,最後運行您的驗證UI線程上的代碼。

這是一個很好的短而整潔的方式來處理您需要的事件和時間。您可以通過安裝「Rx-Main」&「Rx-WinForms」包來通過NuGet添加Rx庫。