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

时间:2019-10-25 04:00:05

标签: 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|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#突出显示。我需要它来处理“查找表单”,就像在记事本中一样:)

我当前的“查找表格”代码:

    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;
        }
    }

它工作正常(无语法),但是如果我将语法切换为 ON ,它就会出现故障:(

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

P.S:我知道我之前曾提出过“查找表格”的问题,但这是另一种问题:)

1 个答案:

答案 0 :(得分:1)

因此,如果您决定使用Scintilla.NET,则希望直接从Visual Studio(https://docs.microsoft.com/en-us/nuget/quickstart/install-and-use-a-package-in-visual-studio)安装nuget软件包。然后,您可以通过执行override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "channelCell", for: indexPath) cell.accessoryType = .disclosureIndicator cell.imageView?.frame = CGRect(x: 0, y: 0, width: cell.frame.size.height, height: cell.frame.size.height) cell.imageView?.contentMode = .scaleToFill cell.imageView?.layer.cornerRadius = cell.frame.size.height / 2 cell.imageView?.clipsToBounds = true cell.imageView?.kf.setImage(with: URL(string: channels[indexPath.row].userUrlImage)) return cell } using ScintillaNET;来创建一个新文件。

我在https://github.com/HicServices/RDMP/blob/f85f1c7f03bc0cdcbabe7ef83d12fa1f4d25bdae/Reusable/ReusableUIComponents/ScintillaHelper/ScintillaTextEditorFactory.cs

的代码库(C#)中使用了它

摘录

new Scintilla()

一旦显示,您可以在https://github.com/jacobslusser/ScintillaNET/wiki/Custom-Syntax-Highlighting

上查看有关自动代码突出显示的文档