zoukankan      html  css  js  c++  java
  • 设置代码Code高亮显示成蓝色

      下面方法是让设置的关键字高亮显示,考虑到了注释与字符串的影响,所以备用,以便将来能够用到.

            private static void ColorizeCode(RichTextBox rtb)
            {
                string[] keywords = {"as", "do", "if", "in", "is", "for", "int", "new", "out", "ref", "try", "base",
                                    "bool", "byte", "case", "char", "else", "enum", "goto", "lock", "long", "null",
                                    "this", "true", "uint", "void", "break", "catch", "class", "const", "event", "false",
                                    "fixed", "float", "sbyte", "short", "throw", "ulong", "using", "where", "while",
                                    "yield", "double", "extern", "object", "params", "public", "return", "sealed",
                                    "sizeof", "static", "string", "struct", "switch", "typeof", "unsafe", "ushort",
                                    "checked", "decimal", "default", "finally", "foreach", "partial", "private",
                                    "virtual", "abstract", "continue", "delegate", "explicit", "implicit", "internal",
                                    "operator", "override", "readonly", "volatile",
                                    "interface", "namespace", "protected", "unchecked",
                                    "stackalloc",
                                    "from", "in", "where", "select", "join", "equals", "let", "on", "group", "by",
                                    "into", "orderby", "ascending", "descending", "var"};
                string text = rtb.Text;
    
                rtb.SelectAll();
                rtb.SelectionColor = rtb.ForeColor;
    
                foreach (String keyword in keywords)
                {
                    int keywordPos = rtb.Find(keyword, RichTextBoxFinds.MatchCase | RichTextBoxFinds.WholeWord);
                    while (keywordPos != -1)
                    {
                        int commentPos = text.LastIndexOf("//", keywordPos, StringComparison.OrdinalIgnoreCase);
                        int newLinePos = text.LastIndexOf("
    ", keywordPos, StringComparison.OrdinalIgnoreCase);
                        int quoteCount = 0;
                        int quotePos = text.IndexOf(""", newLinePos + 1, keywordPos - newLinePos, StringComparison.OrdinalIgnoreCase);
                        while (quotePos != -1)
                        {
                            quoteCount++;
                            quotePos = text.IndexOf(""", quotePos + 1, keywordPos - (quotePos + 1), StringComparison.OrdinalIgnoreCase);
                        }
    
                        if (newLinePos >= commentPos && quoteCount % 2 == 0)
                            rtb.SelectionColor = Color.Blue;
    
                        keywordPos = rtb.Find(keyword, keywordPos + rtb.SelectionLength, RichTextBoxFinds.MatchCase | RichTextBoxFinds.WholeWord);
                    }
                }
    
                rtb.Select(0, 0);
            }
  • 相关阅读:
    解决:Server IPC version 9 cannot communicate with client version 4
    解决Exception: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z
    hadoop解决windows下:Failed to set permissions of path: mp .staging to 0700
    find命令的使用
    linux解决端口冲突问题
    linux中实用的小工具lrzsz
    nginx常用命令
    编写测试用例的方法
    selenium之web自动化模拟操作(窗口,鼠标,键盘,js)
    什么是面向连接服务?什么是无连接服务?它们的区别是什么?
  • 原文地址:https://www.cnblogs.com/cang/p/4253450.html
Copyright © 2011-2022 走看看