0

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.

0

3 Answers 3

2

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

1
  • if i add the text like rtextbox.Text = "<Bold>Hello</Bold>"; it prints the Hello in same text, no bold, nothing, but if i hard code it in xaml, it works. Jan 9, 2012 at 22:12
0

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)
0
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.