Webbrowser does not load a document

I am trying to load an html document into a WebBrowser control, but I am on my way. Here's a sample:

public void Button_Click(object sender, EventArgs e) { webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_c); webBrowser1.DocumentText = "<html>foo</html>"; // The documenttext property is NOT what was set above. // No exception is thrown. It always "<html></html>\0", however. // This line setting the title throws a HRESULT COM error. webBrowser1.Document.Title = "foobar!"; } 

The wb_c event handler is also not called. A web browser control is defined as a form control. The form itself consists only of a browser and a button.

Does anyone have any ideas? I used to use this class without any problems, but this time .Net gods deny me! My ultimate goal is to print the document, but right now I can't even accept it to accept my HTML. Maybe I need holy water or something like that.

Edit: if the title bar is deleted above, the wb_c event handler never fires. It's like something is wrong with the COM component itself, or something like that.

Edit 2: by popular request, here is the full code of my code.

 public partial class Form2 : Form { [STAThread] public static void Main(string[] args) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form2()); } public Form2() { InitializeComponent(); webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_c); } void wb_c(object sender, WebBrowserDocumentCompletedEventArgs e) { throw new Exception("The method or operation is not implemented."); } private void button1_Click(object sender, EventArgs e) { webBrowser1.DocumentText = "<html>foo</html>"; } } partial class Form2 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.webBrowser1 = new System.Windows.Forms.WebBrowser(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // webBrowser1 // this.webBrowser1.Location = new System.Drawing.Point(12, 12); this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20); this.webBrowser1.Name = "webBrowser1"; this.webBrowser1.Size = new System.Drawing.Size(117, 99); this.webBrowser1.TabIndex = 0; // // button1 // this.button1.Location = new System.Drawing.Point(90, 165); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 1; this.button1.Text = "button1"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form2 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(292, 266); this.Controls.Add(this.button1); this.Controls.Add(this.webBrowser1); this.Name = "Form2"; this.Text = "Form2"; this.Load += new System.EventHandler(this.Form2_Load); this.ResumeLayout(false); } #endregion private System.Windows.Forms.WebBrowser webBrowser1; private System.Windows.Forms.Button button1; } 

This is a .Net 2.0 project, launched in VS 2005. System.Windows.Forms.dll is v2.0.50727.

EDIT 3: Adding this line to the end of the Form2 constructor:

 webBrowser1.Navigate("about:blank"); 

It starts the event handler, but it does not affect the behavior of the code when setting the text of the document. Setting a breakpoint after the line webBrowser1.Document.Text still gives the same line "\ 0", and trying to set the header still gives COM HERROR.

+4
source share
4 answers

Try moving the line:

 webBrowser1.Document.Title = "foobar!"; 

into your wb_c method. I think the problem is that when you call it, the Document property is not set yet, and you get an exception with a null reference. If you wait for the page to load, you should be fine.

UPDATE: I tried your sample, and the IS event handler is called, however I suspect that it was called from another thread. Therefore, it falls into the line in which the Exception is thrown, but you never see it, because it is in another thread. Take out the line that throws the exception and replace it with:

 webBrowser1.Document.Title = "foobar!"; 

That should do the trick.

+3
source

Before you can manipulate a document, you need to execute a navigation command. To use WebBrowser to create an HTML page from scratch, just go to "about: blank" like this:

 WebBrowser browser = new WebBrowser(); browser.Navigate("about:blank"); browser.Document.Write(html); 

Then use InnerHtml in the root element, not the DocumentText property, to apply the Html as it is.

+6
source

Downloading a document is asynchronous, so by the time you set the title, there is no guarantee that the document is actually loaded. You need to process the appropriate browser events to detect when the navigation is complete before attempting to modify the document.

Update

In all situations when I used the browser, I had to go to the about:blank page first before I could change the document. Perhaps you should try this before installing DocumentText .

0
source

I use the method above, which uses about: blank and works fine! Yesterday I published an article about this method, and today I just found this topic here :) My article is here: http://starikovs.com/2009/11/25/set-html-webbrowser-csharp/

0
source

All Articles