【发布时间】:2017-03-29 13:35:16
【问题描述】:
所以,我正在尝试构建一个RichTextBox,它允许我在其中编写代码并根据 C# 编程语言更改关键字的颜色。
首先,我已经声明了这一点:
string[] palavraschave = { "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue", "decimal", "default",
"delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto",
"if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out",
"override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc",
"static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort",
"using", "virtual", "void", "volatile", "while", "add", "alias", "ascending", "descending", "dynamic", "from", "get", "global", "group",
"into", "join", "let", "orderby", "partial", "remove", "select", "set", "value", "var", "where", "yield" };
那我有办法检查RichTextBox:
private void CheckRichTextBox(string word, Color color, int startIndex)
{
if (this.rchBoxText.Text.Contains(word))
{
int index = -1;
int selectStart = this.rchBoxText.SelectionStart;
while ((index = this.rchBoxText.Text.IndexOf(word, (index + 1))) != -1)
{
this.rchBoxText.Select((index + startIndex), word.Length);
this.rchBoxText.SelectionColor = cor;
this.rchBoxText.Select(selectStart, 0);
this.rchBoxText.SelectionColor = Color.Black;
}
}
}
要调用它,我会在RichTextBoxTextChanged事件中使用这段代码:
this.CheckRichTextBox(palavraschave.ToString(), Color.Blue, 0);
但它不起作用。我做错了什么?
【问题讨论】:
-
您是否明确需要自己实现?代码高亮有很多可行的解决方案
标签: c# winforms syntax-highlighting