How to show html contents with a RichTextBox?

I want to show html contents in my form. I tried to it with rich text box.

rtBox.Text = body;

but it fails.

How to show html contents in RichTextBox? I am using VS 2008.


如果您拥有HTML内容,则可以使用WebBrowser控件 - 否则,您必须将HTML转换为RTF才能在RichTextBox中呈现


Use a hidden WebBrowser Control and load it with the html content you want. Then SelectAll() from the WebBrowser, Copy(), and Paste() into the richtextbox.

WebBrowser wb = new WebBrowser(); wb.Navigate("about:blank");
string url=@"http:....";
wb.Navigate(url);
private const int sleepTimeMiliseconds = 200;

while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Thread.Sleep(sleepTimeMiliseconds);
Application.DoEvents();
}

wb.Document.ExecCommand("SelectAll", false, null);
wb.Document.ExecCommand("Copy", false, null);
richtextbox.Paste();

RTF encoding is different from HTML. You cannot do this straight away. Rowland has rightly suggested WebBrowser control.

If not, then you need to write your own HTML to RTF converter or find something similar.

链接地址: http://www.djcxy.com/p/45934.html

上一篇: 为什么WriteAllText方法用特殊字符保存XML?

下一篇: 如何使用RichTextBox显示html内容?