We help IT Professionals succeed at work.

Bold or italic charecter in RichTextBox or RTF

Umut Şeker
Umut Şeker asked
on
I have a RichTextBox. I'm assigning the Text like;
....
          myRichTextBox.Text =
                    "Welcome," + Session["Name"] + Environment.NewLine
                    + "Your Age is " + Session["Age"];
....
Everything is ok. The Text shown as;

Welcome, Kosturdur
Your Age is 30

But how can I make the words "Kosturdur" and "30" bold or colored while assigning the value. I know that it's impossibe to do it with Text property so I have to use RTF property of RichTextBox
(RichTextBox.RTF =....).
The problem is I don't know can I do it. I tried but couldn't successed.

Thanks,
Comment
Watch Question

CERTIFIED EXPERT
Commented:
You don't need to use the RTF property. You can simply modify the SelectionFont property when required.....
        myRichTextBox.SelectedText = "Welcome, ";
        myRichTextBox.SelectionFont = new Font(myRichTextBox.Font, FontStyle.Bold);
        myRichTextBox.SelectedText = Session["Name"] + Environment.NewLine;
        myRichTextBox.SelectionFont = new Font(myRichTextBox.Font, FontStyle.Regular);
        myRichTextBox.SelectedText = "Your Age is ";
        myRichTextBox.SelectionFont = new Font(myRichTextBox.Font, FontStyle.Bold);
        myRichTextBox.SelectedText = Session["Age"];

Open in new window

Umut ŞekerSoftware & SQL Server
CERTIFIED EXPERT

Author

Commented:
Thanks webtubbs,
it works great