.net richtextbox 中的语法突出显示,如何处理换行符?
我试图突出显示 richtextcontrol 中的某些语法(引用的文本、XML 标签、XML 注释)。除了我自己的愚蠢造成的明显问题(例如,浏览文本以突出显示语法需要一秒钟的时间,而且我当然无法在键入字符时检查语法),我还遇到了由换行符引起的问题文本。
这是我对语法高亮的无力尝试:(
While i < l
ssRelevantText = t.Text.Substring(i)
ssSelectedText = Regex.Match(ssRelevantText, pattern, RegexOptions.Singleline).Value
idxSelectionStart = t.Find(ssSelectedText, i, RichTextBoxFinds.None)
idxSelectionLength = ssSelectedText.Length
t.SelectionStart = idxSelectionStart
t.SelectionLength = idxSelectionLength
t.SelectionColor = color
i = idxSelectionStart + idxSelectionLength
t.SelectionStart = idxCursorBeforeSelection
t.SelectionLength = 0
t.SelectionColor = color.Black
End While
讽刺的是,我删除了注释,因为 VB 注释破坏了 SO 的语法高亮。)
我还尝试了其他 RegexOptions,但问题仍然存在,例如
“hello”
被发现并准确着色,
“
你好
” “
没有找到。 (在这种情况下,着色也会失败,但并非总是如此。)
如果我用其他符号替换所有换行符,则上述方法有效,并且所有内容都会正确突出显示。为什么换行符会干扰?
或者,如何在 Richtextbox 中进行语法突出显示?我能找到的所有示例都涉及保留字的语法突出显示,而不是基于模式的术语。
编辑:到目前为止,我发现 RichTextBox 方法 Find() 找不到包含换行符的字符串。由于这会触发异常(很容易避免),因此之后不会再进行进一步的突出显示。如何在 RichTextBox 中找到包含换行符的字符串?
I am trying to highlight some syntax in a richtextcontrol (quoted text, XML tags, XML comments). Apart from the obvious problem created by my own stupidity (for example, a run through the text to highlight the syntax takes over a second and I certainly cannot check the syntax whenever a character was typed) I also ran into an issue caused by newlines in the text.
This is my feeble attempt to syntaxhighlight:
While i < l
ssRelevantText = t.Text.Substring(i)
ssSelectedText = Regex.Match(ssRelevantText, pattern, RegexOptions.Singleline).Value
idxSelectionStart = t.Find(ssSelectedText, i, RichTextBoxFinds.None)
idxSelectionLength = ssSelectedText.Length
t.SelectionStart = idxSelectionStart
t.SelectionLength = idxSelectionLength
t.SelectionColor = color
i = idxSelectionStart + idxSelectionLength
t.SelectionStart = idxCursorBeforeSelection
t.SelectionLength = 0
t.SelectionColor = color.Black
End While
(I removed comments because, ironically, VB comments break SO's syntax-highlighting.)
I also tried other RegexOptions but the problem remains that while, for example
"hello"
is found and accurately coloured,
"
hello
"
is not found. (The colouring also fails below such an incident, but not always.)
If I replace all newlines with some other symbol, the above works and everything is highlighted correctly. Why are newlines interfering?
Alternatively, how does one do syntax-highlighting in a richtextbox? All the examples I can find refer to syntax-highlighting of reserved words and not terms based on patterns.
Edit: I so far figured out that the RichTextBox method Find() does not find strings containing newlines. Since that triggers an exception (easily avoided) no further highlighting is done after that. How can I find a string in a RichTextBox that contains newlines?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能想查看此处: http://blogs.microsoft.co.il/blogs/tamir/archive/2006/12/14/RichTextBox-syntax-highlighting.aspx
它提供了一个很好的示例,说明如何使用 RichTextBox 在 C# 中对 higlight 进行语法分析。
You might want to look here: http://blogs.microsoft.co.il/blogs/tamir/archive/2006/12/14/RichTextBox-syntax-highlighting.aspx
It provides a nice example of how to Syntax higlight in C# using a RichTextBox.