HyperX
HyperX

Reputation: 1211

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. 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?

Upvotes: 0

Views: 57

Answers (1)

Sanket
Sanket

Reputation: 20017

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.

Upvotes: 1

Related Questions