C# 输入时的RichTextBox事件

C# 输入时的RichTextBox事件,c#,winforms,events,richtextbox,C#,Winforms,Events,Richtextbox,在RichTextBox中键入内容时,我希望在另一个字符串变量中逐个提取每个单词。它将触发哪个事件,我将如何获得它?请查看。只要控件中的文本发生更改,就会触发此事件。您可以订阅它,并在事件处理程序中拆分文本以分别获取每个单词,如下所示: // subscribe to event elsewhere in your class this.myRichTextBox.TextChanged += this.TextChangedHandler; // ... private void Text

RichTextBox
中键入内容时,我希望在另一个字符串变量中逐个提取每个单词。它将触发哪个事件,我将如何获得它?

请查看。只要控件中的文本发生更改,就会触发此事件。您可以订阅它,并在事件处理程序中拆分文本以分别获取每个单词,如下所示:

// subscribe to event elsewhere in your class
this.myRichTextBox.TextChanged += this.TextChangedHandler;

// ...

private void TextChangedHandler(object sender, EventArgs e)
{
    string currentText = this.myRichTextBox.Text;
    var words = currentText.Split(new [] { ' ' }, 
                                  StringSplitOptions.RemoveEmptyEntries);
    // whatever else you want to do with words here
}
编辑:

如果要获取当前键入的word,只需使用:

如果您担心每次键入拆分单词时的性能/用户体验问题,您可以分析最后一个字符并将其附加到一些
currentWord

// in your event handler
char newestChar = this.myRichTextBox.Text.LastOrDefault();
if (char.IsWhiteSpace(newestChar) || char.IsControl(newestChar))
{
    this.currentWord = ""; // entered whitespace, reset current
}
else 
{
    this.currentWord = currentWord + newestChar;
}

只要您在RichTextBox中键入某个内容,就会引发Simply事件(惊喜!)

好的,这就是我在RichTextBox的TextChanged事件中要做的

string[]x=RichTextBox1.Text.Split(新字符[]{''})


迭代变量(sting数组)x,并将结果放在需要的地方。(如在列表视图、标签等中)

即使TextChanged事件可用于此目的,我认为如果您有一个已填充的文本框,只需输入另一个字符,性能将非常糟糕

因此,要使这项工作真正顺利进行,您需要付出一些额外的努力。可能会在每个TextChanged事件(1000ms)上重新启动计时器,这样用户就不会因为快速输入内容而被拦截,并且只有在触发计时器事件时才开始计算单词

或者考虑将拆分和计数算法放入后台工作程序,如果用户要再次在框中输入内容,则可能(或不)取消计数

更新 好的,下面是一个实现示例:

using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            RestartTimer();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            var textToAnalyze = textBox1.Text;

            if (e.Cancel)
            {
                // ToDo: if we have a more complex algorithm check this
                //       variable regulary to abort your count algorithm.
            }

            var words = textToAnalyze.Split();
            e.Result = words.Length;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled
              || e.Error != null)
            {
                // ToDo: Something bad happened and no results are available.
            }

            var count = e.Result as int?;

            if (count != null)
            {
                var message = String.Format("We found {0} words in the text.", count.Value);
                MessageBox.Show(message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                // ToDo: If we already run, should we let it run or restart it?
                return;
            }

            timer1.Stop();
            backgroundWorker1.RunWorkerAsync();

        }

        private void RestartTimer()
        {
            if (backgroundWorker1.IsBusy)
            {
                // If BackgroundWorker should stop it's counting
                backgroundWorker1.CancelAsync();
            }

            timer1.Stop();
            timer1.Start();
        }
    }
}

但是,先生,不需要存储所有文本,只存储我目前在rtb中键入的单词,直到我按下空格键。奥利弗,我通过按键活动完成了这项工作,非常有成效,感谢您的建议。@user584275:如果有帮助,请投票表决。如果它解决了您的问题,请将其标记为正确。这就是如此运作的方式。
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsFormsApplication
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            RestartTimer();
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            var textToAnalyze = textBox1.Text;

            if (e.Cancel)
            {
                // ToDo: if we have a more complex algorithm check this
                //       variable regulary to abort your count algorithm.
            }

            var words = textToAnalyze.Split();
            e.Result = words.Length;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled
              || e.Error != null)
            {
                // ToDo: Something bad happened and no results are available.
            }

            var count = e.Result as int?;

            if (count != null)
            {
                var message = String.Format("We found {0} words in the text.", count.Value);
                MessageBox.Show(message);
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (backgroundWorker1.IsBusy)
            {
                // ToDo: If we already run, should we let it run or restart it?
                return;
            }

            timer1.Stop();
            backgroundWorker1.RunWorkerAsync();

        }

        private void RestartTimer()
        {
            if (backgroundWorker1.IsBusy)
            {
                // If BackgroundWorker should stop it's counting
                backgroundWorker1.CancelAsync();
            }

            timer1.Stop();
            timer1.Start();
        }
    }
}