C# 如何在保留SelectionColor的同时为RichTextBox创建“查找表单”?C

C# 如何在保留SelectionColor的同时为RichTextBox创建“查找表单”?C,c#,winforms,visual-studio-2013,C#,Winforms,Visual Studio 2013,我正在做一个带有语法突出显示的记事本 我让语法突出显示工作,但现在我需要帮助:D 我需要一个使用此代码的查找表单: // getting keywords/functions string keywords = @"\b(abstract|as|base|break|case|catch|checked|continue|default|delegate|do|else|event|explicit|extern|false|finally|fix

我正在做一个带有语法突出显示的记事本

我让语法突出显示工作,但现在我需要帮助:D

我需要一个使用此代码的查找表单:

            // getting keywords/functions
            string keywords = @"\b(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|volatile|while)\b";
            MatchCollection keywordMatches = Regex.Matches(codeRichTextBox.Text, keywords);

            string purplewords = @"\b(bool|byte|char|class|const|decimal|double|enum|float|int|long|sbyte|short|static|string|struct|uint|ulong|ushort|static|void)\b";
            MatchCollection purplewordsMatches = Regex.Matches(codeRichTextBox.Text, purplewords);

            // getting types/classes from the text 
            string types = @"\b(Console)\b";
            MatchCollection typeMatches = Regex.Matches(codeRichTextBox.Text, types);

            // getting comments (inline or multiline)
            string comments = @"(\/\/.+?$|\/\*.+?\*\/)";
            MatchCollection commentMatches = Regex.Matches(codeRichTextBox.Text, comments, RegexOptions.Multiline);

            // getting strings
            string strings = "\".+?\"";
            MatchCollection stringMatches = Regex.Matches(codeRichTextBox.Text, strings);

            // saving the original caret position + forecolor
            int originalIndex = codeRichTextBox.SelectionStart;
            int originalLength = codeRichTextBox.SelectionLength;
            Color originalColor = Color.Black;

            // MANDATORY - focuses a label before highlighting (avoids blinking)
            menuStrip1.Focus();

            // removes any previous highlighting (so modified words won't remain highlighted)
            codeRichTextBox.SelectionStart = 0;
            codeRichTextBox.SelectionLength = codeRichTextBox.Text.Length;
            codeRichTextBox.SelectionColor = originalColor;

            // scanning...
            foreach (Match m in keywordMatches)
            {
                codeRichTextBox.SelectionStart = m.Index;
                codeRichTextBox.SelectionLength = m.Length;
                codeRichTextBox.SelectionColor = Color.Blue;
            }

            foreach (Match m in purplewordsMatches)
            {
                codeRichTextBox.SelectionStart = m.Index;
                codeRichTextBox.SelectionLength = m.Length;
                codeRichTextBox.SelectionColor = Color.Purple;
            }

            foreach (Match m in typeMatches)
            {
                codeRichTextBox.SelectionStart = m.Index;
                codeRichTextBox.SelectionLength = m.Length;
                codeRichTextBox.SelectionColor = Color.DarkCyan;
            }

            foreach (Match m in commentMatches)
            {
                codeRichTextBox.SelectionStart = m.Index;
                codeRichTextBox.SelectionLength = m.Length;
                codeRichTextBox.SelectionColor = Color.Green;
            }

            foreach (Match m in stringMatches)
            {
                codeRichTextBox.SelectionStart = m.Index;
                codeRichTextBox.SelectionLength = m.Length;
                codeRichTextBox.SelectionColor = Color.Brown;
            }

            // restoring the original colors, for further writing
            codeRichTextBox.SelectionStart = originalIndex;
            codeRichTextBox.SelectionLength = originalLength;
            codeRichTextBox.SelectionColor = originalColor;

            // giving back the focus
            codeRichTextBox.Focus();
这是完整的C突出显示。我需要它来处理Find表单,比如在记事本++: 我当前的查找表单代码:

    public static void Find(RichTextBox rtb, String word, Color color)
    {
        rtb.SelectionStart = 0;
        rtb.SelectionLength = rtb.TextLength;
        rtb.SelectionBackColor = Color.White;
        if (word == "")
        {
            return;
        }
        int s_start = rtb.SelectionStart, startIndex = 0, index;
        while ((index = rtb.Text.IndexOf(word, startIndex)) != -1)
        {
            rtb.Select(index, word.Length);
            rtb.SelectionBackColor = color;
            startIndex = index + word.Length;
        }
    }
它在没有语法的情况下工作得很好,但如果打开语法,它会出现小故障:

如果需要,我可以提供更多信息:


附言:我知道我以前问过一个关于查找表单的问题,但这是一个不同类型的问题:

因此,如果您决定使用闪烁体.NET,那么您希望直接从visual studio安装nuget软件包。然后,您可以使用闪烁网创建一个新的;还有新的闪烁体

我在这里的代码库C中使用了它

这里有一段摘录

            var toReturn =  new Scintilla();
            toReturn.Dock = DockStyle.Fill;
            toReturn.HScrollBar = true;
            toReturn.VScrollBar = true;

            if (lineNumbers)
                toReturn.Margins[0].Width = 40; //allows display of line numbers
            else
                foreach (var margin in toReturn.Margins)
                    margin.Width = 0;

            toReturn.ClearCmdKey(Keys.Control | Keys.S); //prevent Ctrl+S displaying ascii code
            toReturn.ClearCmdKey(Keys.Control | Keys.R); //prevent Ctrl+R displaying ascii code
            toReturn.ClearCmdKey(Keys.Control | Keys.W); //prevent Ctrl+W displaying ascii code

一旦它显示出来,您就可以在此处查看关于自动代码高亮显示的文档

可能值得一看闪烁体,它是一个用于文本编辑器的软件包,是notepad++使用的软件包。除非这是一个技术练习,否则这比从头开始写要容易得多。它内置了lexers,您可以定义自己的lexers。它没有太多的开销,并且有很好的文档。C包是闪烁体.net。你可以通过NuGet安装它。我会尝试一下,然后回来问你:Thomas,我将如何使用它?我从Nuget下载了它,但我找不到一个实际使用它的方法。有工具箱项目吗?或者一个命令?来自他们的文档:你在做代码编辑器吗?但是,Thomas N,我该如何安装最新版本?我在Visual Studio 2013上,因为2019年冷得像疯了一样。托马斯,什么是“行号”?我需要知道!!请toReturn是否应显示在工具箱中?它不适用于meHey行号是一个局部变量,为true表示行号。只要删除else和,如果您只需要返回。边距[0]。宽度=40。toReturn是闪烁体控件。您可以通过执行myform.Controls.AddToReturn将其添加到窗体中。我通常向窗体中添加一个面板,然后在窗体构造函数中初始化Component后调用一个方法,将闪烁体控件添加到面板中。我认为您不能将designer/toolbox与斯金特拉.NET一起使用。