C#中的语法突出显示 [英] Syntax Highlighter in C#

查看:126
本文介绍了C#中的语法突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在制作一种小型,轻量级的编程语言.我有一个解析器和一个编译器经过整理,可以使用了.现在,我只需要脚本的文本编辑器即可.我可以创建一个文本编辑器,但是我需要一个语法突出显示器.我需要一个脚本,该脚本将在文本中搜索单词,并在每次更新文本时更改其颜色.我通常知道如何处理这些事情,但是这一件事确实让我感到难过.有任何建议吗?

Hi,

I am making a small, lightweight programming language. I have got a parser and a compiler sorted out and ready to go. Now I just need a text editor for the scripts. I can make a text editor, but I need a syntax highlighter. I need a script which wil search the text for a word and change the colour of it every time I update the text. I usually know how to go about these things but this one really stumped me. Any suggestions??

推荐答案

1.使用从RichTextBox派生的class.
2.阅读网络上有关RTF规范的一些文章,尤其是有关颜色的部分.
3.创建适当的Regex来找到关键字,并在其周围加上适当的RTF标记.
4.确保对RichTextBoxRtf属性而不是Text运行Regex
5.从override d OnTextChanged protected方法调用代码.
6.尽情享受您的编程语言吧!

P.S.虽然我可以为您提供实际的代码,但我认为最好先自己尝试一下.
如果您无法执行此操作,请过几天在这里添加评论,我会给您代码.
在这里,我们在CodeProject上鼓励程序员自己编写代码.
谢谢您的理解.
1. Use a RichTextBox derived class.
2. Read some articles on the web about RTF specification, especially the part about colors.
3. Create an appropriate Regex to find the keywords and surround them with the appropriate RTF markers.
4. Make sure you run the Regex against the RichTextBox''s Rtf property, not Text!
5. Call the code from the overrided OnTextChanged protected method.
6. Have fun with your programming language!

P.S. While I can give you the actual code, I thought it would be better for you to try it yourself first.
If you are unable to do it, put a comment here in a few days and I''ll give you the code.
Here, at CodeProject, we encourage programmers to code themselves.
Thank you for understanding.




请参阅此CodeProject文章,该文章的控件非常适合您的要求:
用于语法高亮显示的彩色文本框 [
Hi,

see this CodeProject article that has a control that very well suits your requirements:
Fast Colored TextBox for syntax highlighting[^]


尝试此链接

这篇关于C#中的语法突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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