如何在 RichTextBox [C#] 中语法高亮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1697360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:02:52  来源:igfitidea点击:

How to Syntax Highlight in a RichTextBox [C#]?

c#syntax-highlighting

提问by Mohit Deshpande

How do I syntax highlight in a richtextbox control AS THE USER TYPESand USING A String[] keywords. I will be publishing a lightweight notepad to the web soon and I want it to have syntax highlighting. I am using Windows forms. Can someone post a code example?

如何在 Richtextbox 控件中语法突出显示AS THE USER TYPESUSING A String[] 关键字。我将很快向网络发布一个轻量级记事本,我希望它具有语法突出显示。我正在使用 Windows 表单。有人可以发布代码示例吗?

采纳答案by Sam Harwell

回答by Winston Smith

Are you using WinForms or WPF?

您使用的是 WinForms 还是 WPF?

If WPF, you could have a look at AvalonEdit. It's free and open source, and it's used in SharpDevelop (open source IDE).

如果是 WPF,你可以看看AvalonEdit。它是免费和开源的,在 SharpDevelop(开源 IDE)中使用。

回答by Jan

You can change the font of selected words in the richtextbox. Take a look at the Select and SelectedFont properties of the control.

您可以更改富文本框中所选单词的字体。查看控件的 Select 和 SelectedFont 属性。

But essentially, you need to iterate through the words, check if a word is present in your keywords, and then change the font, using the above-mentioned properties.

但本质上,您需要遍历单词,检查关键字中是否存在某个单词,然后使用上述属性更改字体。

回答by Niki

Not exactly an answer to your question, but have you looked at the text editor component from SharpDevelop? It's quite lightweight (<200kB IIRC), can be easily embedded in WinForms applications and has syntax highlighting for several languages built in.

不完全是您问题的答案,但您是否看过SharpDevelop的文本编辑器组件?它非常轻量级(<200kB IIRC),可以轻松嵌入到 WinForms 应用程序中,并且内置了多种语言的语法突出显示。

Otherwise, you might want to look at this CodeProject page. It reformats the RTF while you type, which is not very efficient for large files, and it contains a few creepy catch (Exception) { }blocks, so I'm not sure if I would use it in a life-critical application, but it's definitely a good starting point to see how it can be done.

否则,您可能需要查看此 CodeProject 页面。它会在您键入时重新格式化 RTF,这对于大文件来说效率不高,并且它包含一些令人毛骨悚然的catch (Exception) { }块,所以我不确定是否会在生命攸关的应用程序中使用它,但这绝对是一个很好的起点看看怎么做。

回答by jdehaan

Syntax highlighting is not an easy task to perform efficiently. Many solutions you can find (like the ones involving modification of RTF) are a one time solution. If you want to highlight and un-highlight words on the fly during edition, your code has to be ready for it. I would not reinvent the wheel and use ICSharp.TextEditor or alike to solve your problem.

语法突出显示不是一项容易有效执行的任务。您可以找到的许多解决方案(例如涉及修改 RTF 的解决方案)都是一次性解决方案。如果您想在编辑期间即时突出显示和取消突出显示单词,您的代码必须为此做好准备。我不会重新发明轮子并使用 ICSharp.TextEditor 或类似工具来解决您的问题。

回答by Matt Warren

The Scintillacontrol is an excellent source code editor that includes syntax highlighting amongst a whole range of other features. You can embed it in your own app and there is a .NET wrapperavailable.

所述Scintilla的控制是一个极好的源代码编辑器,包括语法突出显示之间的整个范围的其它特征。您可以将它嵌入到您自己的应用程序中,并且有一个.NET 包装器可用。

With Scintilla you can specify the keywords and it will then apply the syntax highlighting as you type.

使用 Scintilla,您可以指定关键字,然后它会在您键入时应用语法突出显示。