How to show html contents with a RichTextBox?

删除回忆录丶 提交于 2020-01-10 04:16:49

问题


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.


回答1:


If you've got HTML content, you could use the WebBrowser control - otherwise you will have to convert the HTML to RTF to render in the RichTextBox




回答2:


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();



回答3:


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.




回答4:


If you want HTMl highlighted text int he RichTextBox you can use something like this (Syntax Highlighting in Rich TextBox).
From there you can make your own highlighting (based on the html elements).

Here's also an example (An extended RichTextBox to save and load HTML lite files)




回答5:


If you're willing to spend money, the DevExpress RTF control can be given HTML.



来源:https://stackoverflow.com/questions/2627991/how-to-show-html-contents-with-a-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!