0

我正在创建 Windows 窗体,它将验证richtextbox 中的文本并根据给定的要求获取错误。我现在唯一的问题是,每次 Richtextbox 更改验证正在处理时,我的意思是每次按键。我如何设置一个计时器,以便每次用户在richtextbox 中编辑一些文本时,它都会等待几秒钟来验证文本,而不仅仅是他们所做的每一次按键操作。这里要清楚的是我的代码:

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。我怎样才能做到这一点?谢谢

4

3 回答 3

0

使用System.Windows.Forms.Timer

(确保您使用 Forms.Timer 是您希望在 UI 线程上调用 Tick 处理程序)

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
    }
}
于 2012-07-31T07:54:33.947 回答
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;
        }
于 2012-08-01T03:09:00.780 回答
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 库。

于 2012-08-01T03:29:48.577 回答