How to display the string html contents into webbrowser control?

前端 未结 9 1582
不知归路
不知归路 2020-11-27 06:47

I have a c# win app program. I save the text with html format in my database but I want to show it in a webbrowser to my user.How to display the string html contents into we

9条回答
  •  青春惊慌失措
    2020-11-27 07:29

    Here is a little code. It works (for me) at any subsequent html code change of the WebBrowser control. You may adapt it to your specific needs.

        static public void SetWebBrowserHtml(WebBrowser Browser, string HtmlText)
        {
            if (Browser != null)
            {
                if (string.IsNullOrWhiteSpace(HtmlText))
                {
                    // Putting a div inside body forces control to use div instead of P (paragraph)
                    // when the user presses the enter button
                    HtmlText = 
                            @"
                        
                        
                        
                          
    "; } if (Browser.Document == null) { Browser.Navigate("about:blank"); //Wait for document to finish loading while (Browser.ReadyState != WebBrowserReadyState.Complete) { Application.DoEvents(); System.Threading.Thread.Sleep(5); } } // Write html code dynamic Doc = Browser.Document.DomDocument; Doc.open(); Doc.write(HtmlText); Doc.close(); // Add scripts here /* dynamic Doc = Document.DomDocument; dynamic Script = Doc.getElementById("MyScriptFunctions"); if (Script == null) { Script = Doc.createElement("script"); Script.id = "MyScriptFunctions"; Script.text = JavascriptFunctionsSourcecode; Doc.appendChild(Script); } */ // Enable contentEditable /* if (Browser.Document.Body != null) { if (Browser.Version.Major >= 9) Browser.Document.Body.SetAttribute("contentEditable", "true"); } */ // Attach event handlers // Browser.Document.AttachEventHandler("onkeyup", BrowserKeyUp); // Browser.Document.AttachEventHandler("onkeypress", BrowserKeyPress); // etc... } }

提交回复
热议问题