Skip to content Skip to sidebar Skip to footer

Html Formatting In Richtextbox

I've been working with HTML strings obtained from an XML file. I'm trying to figure out a way to display these strings in a richtextbox with formatting. So e.g.

This is

Solution 1:

I've solved it using a WYSIWYG HTML editor. However, I'm facing a new problem, if anyone could help, that'd be great!

Here's my new question: Editing HTML with a WYSIWYG Editor

Solution 2:

Are you using an extended RTB to display html because as far as i know from CodeProject site, .NET RichTextBox control only allows you to save and load files using RTF codes or plain text files. So you need to extend the RTB to get that function.

Here's the link - RTB to save Html.

But if you can avoid it and load xml directly to RTB, its much better as working code is here.

And for more info on html formatting code to RTB, check here

Solution 3:

Winz, I'm a bit lost as to what your requirements are.

Based on your comment...

"I'm trying to display them in a RTB as how they'd appear in on a Web Page"

  1. You want to render the HTML markup as an actual page, for example an <img /> tag renders as an actual image.

  2. You want to render the HTML markup as text as it would appear if you were to "View Page Source" using various syntax colors

The problem...

HTML markup and the RTF specification are vastly different, and therefore you would need to convert the markup to RTF to make a RichTextBox display HTML as a page would appear!

To solve your problems...

  1. To render HTML markup in .NET as a web page you can use the built in WebBrowser control. This control uses Microsoft's IE engine in the background, but be warned, it is not very friendly to certain standards such as HTML5 and CSS3. You can improve how it renders the page using (IIRC) browser feature controls

  2. To render the HTML markup as text, the way it would appear if you were to "View Page Source", using syntax coloring, you can use a RichTextBox to an extent. Here is an example using XML, however since XML and HTML are so similar, you can probably modify the solution for HTML syntax rendering.

Note: Syntax highlighting is basically just rendering text to use certain colors for certain things, I.E. a tag might be in red, whilst an attribute might be in blue, and a comment might be green.

Here is another syntax highlighter example:

http://dotnet-csharp-programming.blogspot.co.uk/2010/04/xml-highlighting-in-rich-textbox.html

Other solutions...

For rendering HTML content as a page, you might consider finding a 3rd party browser control for .NET. I know there is one for the FireFox rendering engine, and there may well be one for Chrome as well. Here is a stackoverflow article about replacing the built in WebBrowser control.

For rendering HTML syntax, you might also consider using Scintilla.NET or you could buy a commercial product such as ActiPro's Syntax Editor

Post a Comment for "Html Formatting In Richtextbox"