繁体   English   中英

键入时的RichTextBox事件

[英]RichTextBox event when typing

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

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

// 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
}

编辑:

如果要获取当前输入的单词,则可以简单地使用IEnumerable.LastOrDefault

var words = currentText.Split(new [] { ' ' }, 
                              StringSplitOptions.RemoveEmptyEntries);
string currentlyTyped = words.LastOrDefault();

如果您担心每次键入时都会出现单词拆分的性能/用户体验问题,则只需分析最后一个字符并将其附加到某些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 TextXhanged Event(惊喜!)

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

string[] x = RichTextBox1.Text.Split(new char[]{ ' ' });

遍历变量(sting数组)x,并将结果放在所需的位置。 (例如在列表视图,标签等中)

即使可以使用TextChanged事件,但我认为如果您已经有一个已填充的TextBox并仅输入另一个字符,性能将很糟糕。

因此,要使它真正正常运行,您需要付出额外的努力。 也许在每个TextChanged事件(1000毫秒)上重新启动计时器,所以不会通过快速输入内容和仅在启动计时器事件时启动字计数来拦截用户。

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

更新

好的,这是一个实现示例:

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();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM