【问题标题】:How to highlight HTML syntax in C# windows form RichTextBox?如何在 C# windows 窗体 RichTextBox 中突出显示 HTML 语法?
【发布时间】:2016-04-20 05:00:46
【问题描述】:

我正在编写一个 c# Html 编辑器应用程序,您可以在其中键入 RichTextBox 控件中的代码。我希望 RichTextBox 的行为类似于 notepad++ 和其他代码编辑器,其中 Html 语法以颜色突出显示,例如:

如何在 C# 窗口中从 RichTextBox 中建立它?我几乎到处搜索,没有找到任何对我有帮助的东西。这是我迄今为止尝试过的,但我没有给出我想要的结果:

private void SyntaxHighlight()
        {
            string[] tags = { "html","head","body","a","b","img","strong","p","h1","h2","h3","h4","h5","h6","embed","iframe","span","form",
                            "button","input","textarea","br","div","style","script","table","tr","td","th","i","u","link","meta","title"};
            foreach (string s in tags)
            {
                richTextBox1.Find("<" + s); 
                richTextBox1.SelectionColor = Color.Blue;
                richTextBox1.Find(">");
                richTextBox1.SelectionColor = Color.Blue;
            }

            string[] attributes = { "href","src","height","width","rowspan","colspan","target","style","onclick","id","name","class"};
            foreach (string s in attributes)
            {
                richTextBox1.Find(s + "=");
                richTextBox1.SelectionColor = Color.Red;
            }
        }

有人可以帮助我吗?我应该在 SyntaxHighlight() 方法中写什么?有人可以给我适当的代码吗? 谢谢

【问题讨论】:

  • 您查看过this 问题及其解决方案吗?
  • @fujiFX 是的,但这不是我想要的,我想突出显示文本而不是背景
  • 您提供的代码得到了什么结果?

标签: c# .net regex richtextbox syntax-highlighting


【解决方案1】:

在您的代码中,您只会找到第一次出现的 HTML 标记并突出显示它。但相反,您应该遍历整个富文本内容以查找相同文本的后续出现。我只是根据您的确切代码进行了快速模拟,请查看。

    private void highlightHTMLText()
    {
        string[] tags = { "html","head","body","a","b","img","strong","p","h1","h2","h3","h4","h5","h6","embed","iframe","span","form",
                        "button","input","textarea","br","div","style","script","table","tr","td","th","i","u","link","meta","title"};
        foreach (string s in tags)
        {
            findAndHighlight("<" + s, Color.Blue);
            findAndHighlight("</" + s, Color.Blue);
            findAndHighlight(">", Color.Blue);
        }

        string[] attributes = { "href", "src", "height", "width", "rowspan", "colspan", "target", "style", "onclick", "id", "name", "class" };
        foreach (string s in attributes)
        {
            findAndHighlight(s + "=", Color.Red);
        }
    }

    private void findAndHighlight(string sSearchStr, Color oColor)
    {
        int index = richTextBox1.Text.IndexOf(sSearchStr);
        while (index != -1)
        {
            richTextBox1.Select(index, sSearchStr.Length);
            richTextBox1.SelectionColor = oColor;

            index = richTextBox1.Text.IndexOf(sSearchStr, index + sSearchStr.Length);
        }
    }

进一步根据this 的回答,您应该能够使用 Notepad++ 本身使用的相同实用程序库Scintilla。正如所指出的,您不需要重新发明轮子,但作为开发人员,我显然更喜欢我自己的工具(它只是我;))。希望这会有所帮助。

【讨论】:

  • 谢谢。我现在要试试你的代码。至于 Scintilla,我不知道如何将其导入我的项目中。你能告诉我怎么做吗?
  • 我自己也没有使用过 Scintilla。刚刚浏览了他们 CodPlex 页面中提供的详细信息,似乎我们应该能够使用提供的 Scintilla 控制器而不是RichTextBox。请在其父网站上阅读this pageScintilla Documentation
【解决方案2】:

Find 不会移动光标,它会返回第一个匹配项的位置。试试这个:

How to select text from the RichTextBox and then color it?

【讨论】:

    猜你喜欢
    • 2016-10-07
    • 2013-11-13
    • 2010-10-16
    • 1970-01-01
    • 2011-08-26
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多