How to add a rtf formatting in the text string for a Richt Text Box?
Asked Answered
K

2

5

In a .NET project I have a long plain text build as concatenation of several strings

StringLongText = text1 + text2 + ... + textN

each string "textX" is the result of an evaluation. At the end of a started process, I show the result in a RichTextBox doing simply:

rtfTxt.Text = StringLongText

All works fine, but I am not able to add any text formatting in the previous text1, text2, textN blocks.

For example I would like to write some words in bold or italic including the formatting directly in the strings before concatenating them.

Doing

text1 = "This is some {\b bold} text"
rtfTxt.Text = text1

did not work.

Any suggestion? Thank you for your help.

Kassie answered 7/11, 2012 at 8:29 Comment(0)
S
8
rtfTxt.Rtf = @"{\rtf1\ansi This is some \b bold\b0 text.}";
Serigraph answered 7/11, 2012 at 8:33 Comment(2)
Thank you, that seems to be the right way. Anyway I need to write some extra code in order to format the string: for example a CrLf new line must be inserted in the right way in the rtf plain text (i.e. \par) otherwise I get an endless line from my string. The problem is that the strings are very complex and created by different objects: I should add the rtf tag direclty in the methods of those objects, but I do not like the idea because I prefer to take separate the implementation. Maybe I should change the design of those objects replacing directly the strings with rtf objects.Kassie
Is there some control to download who can do the job - in case of code in textbox ?Colettacolette
W
0

Just write required content with formatting to RTF File. Load RTF file in richtextbox. Verify. Read RTF content into the textbox. Put textbox content to RTF as:

Richtext.rtf = textbox1.text; // (which is textual content)
Worldwide answered 9/2, 2015 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.