键入时的 RichTextBox 事件
当我在 RichTextBox
中输入内容时,我想在另一个字符串变量中逐个获取每个单词。哪个事件将被触发?我将如何获得它?
I want to fetch each word individually one by one in another string variable when I type something in a RichTextBox
. Which event will be fired on it and how will I get it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看一下 TextChanged 事件。每当控件中的文本发生更改时,就会触发此事件。您可以订阅它,并在事件处理程序中拆分文本以单独获取每个单词,如下所示:
编辑:
如果您想获取当前输入的单词,您可以简单地使用 IEnumerable.LastOrDefault:
如果您担心每次拆分单词时出现性能/用户体验问题输入,您可以只分析最后一个字符并将其附加到某个
currentWord
:Take a look at TextChanged event. Whenever text in control changes, this event fires. You can subscribe to it, and in event handler split your text to get each word individually, like this:
Edit:
If you want to get currently typed word, you could simply use IEnumerable.LastOrDefault:
If you are worried about performance/user experience problems with splitting words everytime you type, you can just analyse last character and append it to some
currentWord
:只需 TextXhanged 每当您在 RichTextBox 中输入一些内容(惊讶!)
Simply TextXhanged Event will be raised whenever you type somthing in the RichTextBox (surprise!)
好的,这就是我在 Richtextbox 的 TextChanged 事件中要做的
string[] x = RichTextBox1.Text.Split(new char[]{ ' ' });
迭代变量(字符串数组) x,并将结果放在您需要的地方。 (如列表视图、标签等)
ok this is what I would do in the TextChanged Event of the Richtextbox
string[] x = RichTextBox1.Text.Split(new char[]{ ' ' });
Iterate through the variable (sting array) x, and place the results where you need it. (like in a listview, a label etc.)
即使 TextChanged 事件可用,我认为如果您有一个已经填充的 TextBox 并只需输入另一个字符,性能也会很糟糕。
因此,要使其真正顺利运行,您需要一些额外的努力。也许在每个 TextChanged 事件(1000 毫秒)上重新启动计时器,这样用户就不会因快速输入内容而被拦截,并且仅在计时器事件触发时才开始字数计数。
或者考虑将分割和计数算法放入后台工作人员中,如果用户要在框中再次输入某些内容,则可能(或不)取消计数。
更新
好的,这是一个实现示例:
Even if the TextChanged event is usable for that i think the performance will be horrible if you have an already filled TextBox and simply enter another character.
So to get this really to work smooth you need some additional efforts. Maybe restart a timer on every TextChanged event (1000ms), so the user won't be intercepted by entering something fast and start the word counting only when the timer event is fired up.
Or think about putting the split and count algorithm into a background worker and maybe (or not) cancel the count if the user is going to enter something again into the box.
Update
Ok, here is an implementation example: