【问题标题】:How to change RichTextBox text depending on a string in C#如何根据 C# 中的字符串更改 RichTextBox 文本
【发布时间】: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


【解决方案1】:

您需要对单词数组执行 for each 并为每个单词调用您的过程,而不是调用 toString。或者将数组传递给您的函数并在那里循环。

但正如其他人所说,对于这类事情已经有很多解决方案了。

【讨论】:

  • 我做到了,但它不能完美地工作,因为当它找到例如“int startindex = 0;”时它会将“int”更改为蓝色,将“in”从索引更改为蓝色,因为在字符串数组中我有“in”。你知道我的意思吗?
  • 也许最好使用正则表达式并检查带有或不带有前导和尾随空格的关键字。
【解决方案2】:

考虑使用ColorCode 之类的东西;这项工作已经做得很好,而且可能更有效率。 为此,请添加 NuGet 包,然后将 RichTextBox 替换为 WebBrowser 控件,并将源文本传递到着色器:

using ColorCode;
[...]
webBrowser1.DocumentText = new CodeColorizer().Colorize(mySourceText, Languages.CSharp);

有关添加 NuGet 包的信息,请参阅here


或者,如果您正在寻找语法高亮文本编辑器,您可以使用 Scintilla。

首先添加一个名为jacobslusser.ScintillaNET的NuGet包,这次多了一个步骤,让控件出现在工具箱中;右键单击 Windows 窗体工具箱,选择“选择项目”,然后选择“浏览”,然后浏览到 Scintilla DLL。 在我的项目中,这里是;

packages\jacobslusser.ScintillaNET.3.6.3\lib\net40\ScintillaNET.dll

这会将“Scintilla”控件添加到工具箱中,您可以将其拖到表单中。最后,在表单的Load 事件中,添加以下“配方”,使其看起来类似于 Visual Studio 的语法高亮:

private void Form1_Load(object sender, EventArgs e)
{
    // Configuring the default style with properties
    // we have common to every lexer style saves time.
    scintilla1.StyleResetDefault();
    scintilla1.Styles[Style.Default].Font = "Consolas";
    scintilla1.Styles[Style.Default].Size = 10;
    scintilla1.StyleClearAll();

    // Configure the CPP (C#) lexer styles
    scintilla1.Styles[Style.Cpp.Default].ForeColor = Color.Silver;
    scintilla1.Styles[Style.Cpp.Comment].ForeColor = Color.FromArgb(0, 128, 0); // Green
    scintilla1.Styles[Style.Cpp.CommentLine].ForeColor = Color.FromArgb(0, 128, 0); // Green
    scintilla1.Styles[Style.Cpp.CommentLineDoc].ForeColor = Color.FromArgb(128, 128, 128); // Gray
    scintilla1.Styles[Style.Cpp.Number].ForeColor = Color.Olive;
    scintilla1.Styles[Style.Cpp.Word].ForeColor = Color.Blue;
    scintilla1.Styles[Style.Cpp.Word2].ForeColor = Color.Blue;
    scintilla1.Styles[Style.Cpp.String].ForeColor = Color.FromArgb(163, 21, 21); // Red
    scintilla1.Styles[Style.Cpp.Character].ForeColor = Color.FromArgb(163, 21, 21); // Red
    scintilla1.Styles[Style.Cpp.Verbatim].ForeColor = Color.FromArgb(163, 21, 21); // Red
    scintilla1.Styles[Style.Cpp.StringEol].BackColor = Color.Pink;
    scintilla1.Styles[Style.Cpp.Operator].ForeColor = Color.Purple;
    scintilla1.Styles[Style.Cpp.Preprocessor].ForeColor = Color.Maroon;
    scintilla1.Lexer = Lexer.Cpp;

    // Set the keywords
    scintilla1.SetKeywords(0, "abstract as base break case catch checked continue default delegate do else event explicit extern false finally fixed for foreach goto if implicit in interface internal is lock namespace new null object operator out override params private protected public readonly ref return sealed sizeof stackalloc switch this throw true try typeof unchecked unsafe using virtual while");
    scintilla1.SetKeywords(1, "bool byte char class const decimal double enum float int long sbyte short static string struct uint ulong ushort void");

}

更多关于 Scintilla here。来自here 的食谱。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 2014-11-30
    • 1970-01-01
    相关资源
    最近更新 更多