Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 195737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:42:27+00:00 2026-05-11T16:42:27+00:00

I’m trying to load an html document into a WebBrowser control, but I’m at

  • 0

I’m trying to load an html document into a WebBrowser control, but I’m at my wits end. 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's 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 never called, either. The webbrowser control is defined as a control on the form. The form itself consists of only the browser and the button.

Does anyone have any ideas? I’ve used this class before without any issues, but this time the .Net gods are denying me! My end goal is to print the rendered document, but right now I can’t even get it to accept my HTML. Maybe I need some holy water or something.

Edit: If the Title line is removed above, the wb_c event handler is never getting triggered. It’s as though there’s something wrong with the COM component itself, or something.

Edit 2: By popular demand, here is a complete blob 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 running 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");

Does trigger the event handler, but it doesn’t otherwise affect the behavior of the code when setting the document text. Setting a breakpoint after the webBrowser1.Document.Text line still gives the same “\0” string, and trying to set the title still gives a COM HERROR.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:42:28+00:00Added an answer on May 11, 2026 at 4:42 pm

    Try moving the line:

    webBrowser1.Document.Title = "foobar!";
    

    into your wb_c method. I think the problem is that when you’re calling it, the Document property isn’t completley set yet, and you’re getting a null reference exception. If you wait till the page is loaded, you should be ok.

    UPDATE: Tried your sample, and your event handler IS getting called, however I suspect it’s being called from another thread. Therefore, it gets to the line where the Exception is thrown, but you’re never actually seeing it, because it’s in another thread. Take out the line that throws the exception, and replace it with:

    webBrowser1.Document.Title = "foobar!";
    

    That should do the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 128k
  • Answers 128k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The functionality is already there, no need to resort to… May 12, 2026 at 5:42 am
  • Editorial Team
    Editorial Team added an answer Have you considered HDLC or SDLC? There's also LAP/D (Link… May 12, 2026 at 5:42 am
  • Editorial Team
    Editorial Team added an answer I figured it out. In the particular scenario above, I… May 12, 2026 at 5:42 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.