0

I am blocked in using the WebBrowser document for MouseDown event Handler.

Designer:

this.webBrowser.TabIndex = 4;
this.webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.WebBrowser_DocumentCompleted);

Form class:

private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    webBrowser.Document.MouseDown += new HtmlElementEventHandler(webBrowser_MouseDown);
}

private void webBrowser_MouseDown(object sender, HtmlElementEventArgs e)
{
}

In the code above, when loading the first URL selected, it will call DocumentCompleted.
According to documentation, the WebBrowser's document should be complete when this event is raised.

But I meet an error because webBrowser.Document is null and I didn't find any explanation.

Is there a way to retrieve the loaded Document in DocumentCompleted?

Jimi
  • 29,621
  • 8
  • 43
  • 61
  • Read the notes here: [Getting mouse click coordinates in a WebBrowser Document](https://stackoverflow.com/a/54232729/7444103) and here: [How to get an HtmlElement value inside Frames/IFrames?](https://stackoverflow.com/a/53218064/7444103). – Jimi Feb 02 '19 at 17:40
  • Thank you Jimi for these links. That's pretty much what I found online. However, after I red these, I checked in my code what was the value of ReadyState. Apparently, the ReadyState is kept Uninitialized in case the link is a local URI, and not http address. Is there a way to initialise the document in case of local address (C://)? – Aurelien.P Feb 02 '19 at 18:07
  • The document source is not important. It could be a remote Https resource, a localhost http resource of a local file. The `DocumentCompleted` event is raised indifferently. Its `ReadyState` is set according to the state of the Document load process. If you still have some problems, post the code you're using now. – Jimi Feb 02 '19 at 19:39
  • Cannot reproduce error you get – Zam Feb 02 '19 at 19:56

0 Answers0