在格式文本框在.NET颜色文本 [英] Color Text in Rich Text Box in .NET

查看:176
本文介绍了在格式文本框在.NET颜色文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有丰富的文本框编辑XML文本 我想如何着色在RichTextBox内部的XML标签名称是什么
我想红色或绿色的颜色标签名称。
没有办法做到这一点?

I have Rich TextBox to Edit XML Text What i want how to color the XML Tags Names inside the RichTextBox
I want the tags names in RED or Green color.
Any Way to do that?

推荐答案

工作什么你想要的正则表达式使用的此页面。一旦你有了这个,你可以使用类似下面的方法来更新的RichTextBox

Work out what your desired regex is using this page. Once you have this you could use something like the following method to update the RichTextBox

public static void HighlightSyntax(RichTextBox richTextBox, Regex yourRegex, Color someColor)
{
    richTextBox.BeginUpdate();
    int selPos = richTextBox.SelectionStart;
    richTextBox.SelectAll();
    richTextBox.SelectionColor = normTextColor;
    richTextBox.Select(selPos, 0);

    // For each match from the regex, highlight the word.
    foreach (Match keyWordMatch in yourRegex.Matches(richTextBox.Text))
    {
        richTextBox.Select(keyWordMatch.Index, keyWordMatch.Length);
        richTextBox.SelectionColor = someColor;
        richTextBox.Select(selPos, 0);
        richTextBox.SelectionColor = normTextColor;
    }
    richTextBox.EndUpdate();
}

您也可以通过一个定时器后自动设定时间来更新这个。

You could also adopt a timer to update this automatically after a set time.

我希望这有助于。

请注意。对于大的文本文件,而像这样的做法将是缓慢的!在这种情况下,我会采取Sinctilla.NET为如下...

Note. For large text files, and approach like this will be slow! In this case I would adopt Sinctilla.NET as a full syntax highlighter as stated in one of the answers below...

这篇关于在格式文本框在.NET颜色文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆