如何在写作时突出显示ritchtextbox vb.net中的一些单词 [英] how to highlight some words in ritchtextbox vb.net while writing

查看:116
本文介绍了如何在写作时突出显示ritchtextbox vb.net中的一些单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello guys

我想创建一个代码编辑器,在编写时突出显示我的关键字。

我应该制作一个每秒扫描整个richtextbox的方法(使用计时器) - (这对性能来说是坏事)还是有另一种方法?

非常感谢,对不起我的经验感到抱歉:(

Hello guys
I would like to make a code editor that highlight my keywords while writing.
should I make a method that scan the whole richtextbox every second (using a timer) -(and that is bad bad for performance ) or is there another methode ?
thanks a lot in advance and sorry for my lack of experience :(

推荐答案

当然你是对的:定时器轮询......不好当然,你不应该这样做。



这是我的推销:你的逻辑在哪里?跟我来说:如果你已经准备好处理一些事件了更新属性,为什么不寻找其他事件,更合适的事件。但是什么事件?显然,只有那些本质上有任何改变的东西。轮询的主要问题是:没有任何改变为控制内容,但你仍在轮询可能的变化,这是坏的。什么事件可能会改变这个控件的内容?当然,输入事件。如果用户没有输入任何东西(包括删除一些文本/对象),没有任何改变,没有什么可以找。



你明白了吗?;-)







让我们迈出下一步。我们可以只选择一个事件,唯一值得为您的目的处理的事件吗?这是事件 SelectionChanged 。更好的是,您可以覆盖虚拟方法 OnSelectionChanged

http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionchanged.aspx [<一个href =http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionchanged.aspxtarget =_ blanktitle =New Window> ^ ],

http://www.codeproject.com/script/Reputation/ List.aspx?mid = 2291164 [ ^ ]。



您无法在不更改选择的情况下更改控件的内容;即使你从未选择任何东西,如果你移动插入符号,这意味着属性 SelectionStart 是更改。我告诉你,这只是选择。



为什么这个活动真好?你不会冒险陷入无限递归。如果你突出显示,你就不会改变选择。



但如果你有一些算法改变选择,你应该关闭处理内部处理(使用一些标志应该是您控制'的属性),更改处理程序内的选择,并在完成后恢复处理标志。换句话说,让它不可重复。



问题解决了。



祝你好运,



-SA
Of course you are right: polling by timer… bad idea. Of course, you should not do it.

Here is my pitch: where is your logic? Follow me: if you are already ready to handle some events to update the properties, why not looking for some other events, more suitable ones. But what events? Apparently, only those which change anything essentially. The main problem with polling is: nothing has changed to the control content, but you are still polling for possible change, which is bad. And what events could possibly change the content of this control? Of course, input events. If the user does not input anything (including deletion of some text/objects), nothing is changed, nothing to look for.

Are you getting the idea? ;-)



Let''s make a next step. Can we choose just one event, the only one worth handling for your purposes? This is the event SelectionChanged. Even better, you can override the virtual method OnSelectionChanged:
http://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.selectionchanged.aspx[^],
http://www.codeproject.com/script/Reputation/List.aspx?mid=2291164[^].

You cannot possible change the content of the control without changing the selection; even of you never select anything, if you move the caret, it means the property SelectionStart is changes. This is just the selection, I tell you.

Why this event is so good? You are not risking to fall into an infinite recursion. If you do you highlighting, you don''t change selection.

But if you have some algorithm changing selection, you should switch off the handling inside handling (use some flag which should be you control''s property), change selection inside the handler, and restore processing flag after you are done. In other words, make it non-reentrable.

Problem solved.

Good luck,

—SA


这篇关于如何在写作时突出显示ritchtextbox vb.net中的一些单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆