简体   繁体   English

为richTextBox中的文本着色

[英]Coloring part of text in richTextBox

So i am currently writing a small IRC bot for Twitch, and i am doing it with WPF, and i would like to color only username in the text line i add to richTextBox. 因此,我目前正在为Twitch编写一个小型IRC机器人,并且正在使用WPF进行操作,我想仅对添加到richTextBox中的文本行中的用户名进行着色。 I tryed with simple Foreground coloring but it colors me everytime everything. 我尝试使用简单的前景着色,但是它使我每次都着色。

My current code: 我当前的代码:

if (e.ChatMessage.ColorHex.StartsWith("#"))
{
     richTextBox.Foreground = ChatUtils.convertHexToBrush(e.ChatMessage.ColorHex);
}

richTextBox.AppendText(String.Format("[{0}] <{1}>: {2}", 
                       DateTime.Now.ToString("HH:mm:ss"),  
                       e.ChatMessage.DisplayName, e.ChatMessage.Message) + "\n");

richTextBox.ScrollToEnd();

So how would i color only the parameter {1} which is e.ChatMessage.DisplayName ? 那么我如何只给参数{1} e.ChatMessage.DisplayName ,即e.ChatMessage.DisplayName

Try like this- 尝试这样-

TextRange tr = new TextRange(rtb.Document.ContentEnd,­ rtb.Document.ContentEnd);
tr.Text = e.ChatMessage.DisplayName;
tr.ApplyPropertyValue(TextElement.­ForegroundProperty, Brushes.Red);

See if this helps. 看看是否有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM