gpt4 book ai didi

c# - .NET 中富文本框中的彩色文本

转载 作者:行者123 更新时间:2023-11-30 22:24:02 24 4
gpt4 key购买 nike

我有 Rich TextBox 来编辑 XML 文本我想要的是如何为 RichTextBox 中的 XML 标签名称着色
我想要红色或绿色的标签名称。
有什么办法吗?

最佳答案

算出你想要的正则表达式正在使用 this page .一旦你有了这个,你可以使用类似下面的方法来更新 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();
}

您也可以采用定时器在设定时间后自动更新。

希望对您有所帮助。

注意。对于大文本文件,这样的方法会很慢!在这种情况下,我会采用 Sinctilla.NET 作为完整的语法荧光笔,如以下答案之一所述...

关于c# - .NET 中富文本框中的彩色文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13007560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com