Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#windows窗体RichTextBox中突出显示HTML语法?_C#_.net_Regex_Richtextbox_Syntax Highlighting - Fatal编程技术网

如何在C#windows窗体RichTextBox中突出显示HTML语法?

如何在C#windows窗体RichTextBox中突出显示HTML语法?,c#,.net,regex,richtextbox,syntax-highlighting,C#,.net,Regex,Richtextbox,Syntax Highlighting,我正在编写一个c#Html编辑器应用程序,您可以在其中的RichTextBox控件中键入代码。我希望RichTextBox的行为类似于记事本++和其他代码编辑器,其中Html语法以颜色突出显示,例如: 如何在C#windows窗体RichTextBox中建立此功能?我几乎找遍了所有地方,没有找到任何对我有帮助的东西。这是我迄今为止尝试过的,但我没有给出我想要的结果: private void SyntaxHighlight() { string[] ta

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

如何在C#windows窗体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;
            }
        }
private void SyntaxHighlight()
{
string[]标记={“html”、“head”、“body”、“a”、“b”、“img”、“strong”、“p”、“h1”、“h2”、“h3”、“h4”、“h5”、“h6”、“embed”、“iframe”、“span”、“form”,
“按钮”、“输入”、“文本区”、“br”、“div”、“样式”、“脚本”、“表”、“tr”、“td”、“th”、“i”、“u”、“链接”、“元”、“标题”};
foreach(标记中的字符串s)
{
richTextBox1.Find(“”);
richTextBox1.SelectionColor=Color.Blue;
}
string[]attributes={“href”、“src”、“height”、“width”、“rowspan”、“colspan”、“target”、“style”、“onclick”、“id”、“name”、“class”};
foreach(属性中的字符串s)
{
richTextBox1.查找(s+“=”);
richTextBox1.SelectionColor=Color.Red;
}
}
有人能帮我吗?我应该在SyntaxHighlight()方法中写什么?有人能给我合适的代码吗?
谢谢

查找不移动光标,它返回第一个匹配的位置。请尝试以下方法:


查找不移动光标,它返回第一个匹配的位置。请尝试以下方法:


在代码中,您只找到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);
        }
    }
private void highlightHTMLText()
{
string[]标记={“html”、“head”、“body”、“a”、“b”、“img”、“strong”、“p”、“h1”、“h2”、“h3”、“h4”、“h5”、“h6”、“embed”、“iframe”、“span”、“form”,
“按钮”、“输入”、“文本区”、“br”、“div”、“样式”、“脚本”、“表”、“tr”、“td”、“th”、“i”、“u”、“链接”、“元”、“标题”};
foreach(标记中的字符串s)
{

findAndHighlight(“在您的代码中,您只找到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);
        }
    }
private void highlightHTMLText()
{
string[]标记={“html”、“head”、“body”、“a”、“b”、“img”、“strong”、“p”、“h1”、“h2”、“h3”、“h4”、“h5”、“h6”、“embed”、“iframe”、“span”、“form”,
“按钮”、“输入”、“文本区”、“br”、“div”、“样式”、“脚本”、“表”、“tr”、“td”、“th”、“i”、“u”、“链接”、“元”、“标题”};
foreach(标记中的字符串s)
{

查找并突出显示(“您签出问题及其解决方案了吗?@fujiFX是的,但这不是我想要的,我想突出显示文本而不是背景。您使用提供的代码得到的结果是什么?您签出问题及其解决方案了吗?@fujiFX是的,但这不是我想要的,我想突出显示文本而不是背景。结果是什么你正在使用你提供的代码?谢谢。我现在要试用你的代码。至于斯金塞拉,我不知道如何将其导入到我的项目中。你能告诉我如何导入吗?我自己也没有使用斯金塞拉。我只是浏览了他们的CodPlex页面中提供的详细信息,似乎我们应该能够在中使用提供的斯金塞拉控制器代替
RichTextBox
。请继续阅读,以及他们的母网站。谢谢。我现在要试试你的代码。至于斯金塞拉,我不知道如何将它导入到我的项目中。你能告诉我怎么做吗?我自己也没有使用过斯金塞拉。只是浏览了他们的CodPlex页面中提供的详细信息,似乎我们应该这样做能够使用提供的闪烁控制器,而不是
RichTextBox
。请在其母网站上阅读。