20

I'm developing a win-form application that needs sometime to show a "pop-up" form that displays a portion of a web page on internet (HTML). I'm getting the HTML of the page using a classic web request:

 var serviceRequest = new WebClient();
 var response = serviceRequest.DownloadString(new Uri("www.something.com"));

I have already tried to use the web browser control which works really well but as you know there are several issue using it as it is based on a COM object.(I cannot dynamically create another form that contains the web browser control without create a thread STA etc)

All I need is "container" where I can inject the HTML I want to display.

Any suggestions?

thanks a lot

Massimiliano Peluso
  • 26,379
  • 6
  • 61
  • 70
  • 1
    Note: Strictly speaking, this question is about specifically _avoiding_ the use of the WinForms [`WebBrowser` control](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.webbrowser). However, the question's generic title has prompted answers recommending precisely that control - and searching the web for related keywords is likely to bring readers here for whom use of that control _is_ an option. – mklement0 Oct 20 '19 at 19:31

3 Answers3

37

You can user web browser control.It can inject all html code directly.

webBrowser1.DocumentText = "<html>hello <script>alert('hi');</script></html>";
Burmese Bug
  • 737
  • 1
  • 6
  • 7
15

Try to use built-in WebBrowser control.

References at CodeProject:

For Managed HTML Rendering, see Professional HTML Renderer

For Dynamic HTML Rendering, see Show Dynamic HTML in WinForm Applications

Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
  • 1
    I have already tried the HtmlRenderer but it seems does not show the page css correctly.+1 anyway for the good suggestion The second link uses a webbrowser control which I would not like to use as explained in the question – Massimiliano Peluso Oct 04 '12 at 09:32
  • I am afraid that there is no way aside to render HTML to WinForms application and Professional HTMl Renderer is a good source of information for your case but use should refer to this link also http://www.codeproject.com/Articles/12172/An-extended-RichTextBox-to-save-and-load-quot-HTML – Furqan Safdar Oct 04 '12 at 09:45
  • I have found yet another link http://www.webprogrammingblog.com/c-sharp-firefox-web-browser-geckofx/ from this link http://stackoverflow.com/questions/7514328/how-to-show-html-formatted-content-without-image-in-a-winform – Furqan Safdar Oct 04 '12 at 09:57
  • There is also [this](http://www.modeltext.com/html/index.aspx) HTML editor control, though it requires xhtml. I want to improve the HTML Renderer, can you provide the HTML\CSS that didn't work for you, Thx. – Arthur Oct 29 '12 at 16:21
0
webBrowser1.Document.Write(yourHtmlText);
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123