Using richtextbox as a chat window which displays

2019-09-05 23:42发布

I am making a C# WPF chat messenger, i have used Wpf rich text box for displaying chat, but i am trying to customize nick and do some text alignment, rich text box having text is made by appending text using code, i dont know how to customize it, i am attaching an image for more explanation
enter image description here

I want this date to be left aligned, and i want the nick name should be blue in color. I think we cannot use HTML Text in rich text box, or whats the solution for customizing the text, should i use some tags, or what, please let me know the better solution.

3条回答
smile是对你的礼貌
2楼-- · 2019-09-05 23:50

I thought that in code you have to use /b for bold. So in this case, your code would be like

rtextbox.Text = "/bHello/b";

Let me know if it worked.

Edit because the first solution didn't work :

1 Select the text that you want to transform :

rtb.select(Start , Length of string)

2 Create a font with the right properties and add it to the selection

rtb.Selectionfont = new Font(rtb.SelectionFont, FontStyle.Bold)
查看更多
聊天终结者
3楼-- · 2019-09-06 00:06

Create a new Span every time and add it instead of appending text.

            Span nick = new Span();
            nick.Foreground = Brushes.Blue;

            Span date = new Span();
            date.FontWeight = FontWeights.Bold;

            Paragraph para = new Paragraph();
            para.Inlines.Add(nick);
            para.Inlines.Add(date);

            FlowDocument d = new FlowDocument();
            d.Blocks.Add(para);

            rtb.Document = d;

Hope this helps.

Regards,

Jawahar

查看更多
爷的心禁止访问
4楼-- · 2019-09-06 00:14

You may be better served by using a FlowDocument and appending to the document's content as each message is sent/received (click for more info).

查看更多
登录 后发表回答