It is impossible to implement the display of the html file in the WebBrowser element, the MSDN article was read, but it does not work. How to implement the code?

  • Okay, what do you have at the moment? Give the code. - VladD
  • I can not give the code as such, because I went through various variations in trying to display html, so I wrote it here hoping for help with displaying the contents in the browser element - Dmitry Korol
  • Okay, let's start with the following: WinForms or WPF? And yet C # or C ++ / CLI? - VladD
  • windows forms C ++ / CLI. As I understand, this is an inappropriate combination? - Dmitry Korol
  • Well, C # would be better. But the idea is not so scary. - VladD

2 answers 2

  1. read the file into a string by calling File.ReadAllText(filename) ;
  2. set the content of WebBrowser to this line - through the DocumentText property:

     webBrowser1.DocumentText = "<html><body>Hello, World!</body></html>" 

Exposing DocumentText works only once. If you need to change the content several times, use the following code:

 private void DisplayHtml(string html) { webBrowser1.Navigate("about:blank"); if (webBrowser1.Document != null) { webBrowser1.Document.Write(string.Empty); } webBrowser1.DocumentText = html; } 
  • Well, I, in fact, need to display the entire html with the formatting of the text, and not consider it only as text. although, perhaps, I misunderstood you, as I am quite a novice. It turns out, it is necessary to register everything here? 'private: System :: Void part4L1_Load (System :: Object ^ sender, System :: EventArgs ^ e) {}' - Dmitry Korol
  • private: System::Void part4L1_Load(System::Object^ sender, System::EventArgs^ e) { непосредственно код } - Dmitry Korol

Try this:

 String^ uriString = String::Format("file:///{0}", <тут путь к вашему html>); webBrowser1->Navigate(gcnew Uri(uriString)); 

In the case where the html is in the directory where the exe (and not where the project is!), Something like this will do:

 String^ dir = Path::GetDirectoryName(Application::ExecutablePath); String^ path = Path::Combine(dir, "тут имя вашего html"); 

and as usual

 String^ uriString = String::Format("file:///{0}", path); webBrowser1->Navigate(gcnew Uri(uriString));