RichTextBox markup formatting?

korell

New member
Joined
Jul 29, 2009
Messages
3
Programming Experience
5-10
I am trying to bind a RichTextBox to data from an XML file where text within a paragraph may be tagged with formatting tags. As an example the text may look like this:
"The size of the area is 5000 m<superscript>2</superscript>".

Text may also contain other formatting tags such as <bold> or <italic>:
"The quick brown fox <bold>jumps</bold> over the <italic>lazy</italic> dog".

I assume that I have to somehow transform the tagged text into a format that the textbox can render correctly (rtf?), but I don't know how.

Can somebody help?

Thanks,
Sacha
 
The easier option is to translate the html code to rtf code with plain search and replace strings like this:
VB.NET:
Dim html As String = "The quick brown fox <bold>jumps</bold> over the <italic>lazy</italic> dog"
Dim build As New System.Text.StringBuilder(html)
build = build.Replace("<bold>", "\b ")
build = build.Replace("</bold>", "\b0 ")
build = build.Replace("<italic>", "\i ")
build = build.Replace("</italic>", "\i0 ")
build.Insert(0, "{\rtf1\ansi\ansicpg1252\deff0\deflang1044{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}\viewkind4\uc1\pard\f0\fs17 ")
build.Append("}")
Me.RichTextBox1.Rtf = build.ToString
Rich Text Format (RTF) Version 1.5 Specification

There exist of course lots of third party libraries that does this, but I'm not aware of any free ones.
 
Thanks to both of you! This was exactly was I was looking for. I tried the replace before, but it didn't work because I didn't know to insert the rtf line ({\rtf1...).

Thanks again,
Sacha
 
I didn't know to insert the rtf line ({\rtf1...).
That line I got from the empty RTB control from Rtf property, whether you assign the complete Rtf string or the partial SelectedRtf string it has to be a complete rtf encoded "document". If you format text in the RTB control you can read the Rtf to see how it encodes it, also refer to documentation what the codes mean.
 
Yes, I guess I didn't understand how to rtf encode the string. I have already bookmarked the RTF specification link you provided. Very valuable information!

Thanks,
Sacha
 
Back
Top