程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 設置代碼Code高亮顯示成藍色,code高亮顯示

設置代碼Code高亮顯示成藍色,code高亮顯示

編輯:C#入門知識

設置代碼Code高亮顯示成藍色,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("\n", 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);
        }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved