2012-07-31 151 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库。