0

I have a form with a webBrowser object named "myBrowser". When I open a webpage in this browser, I would like to get the id of any element that my mouse is hovering over and show it in a textbox named "txtProperties".

I have tried to achieve this using the following code:

Public Class buildBrowser
Dim myHTMLDocument As HtmlDocument

Private Sub btnBrowseSubmit_Click(sender As Object, e As EventArgs) Handles btnBrowseSubmit.Click
    myBrowser.Navigate(txtAddress.Text)
End Sub



Private Sub Document_MouseOver(sender As Object, e As HtmlElementEventArgs)
    txtProperties.Text = TryCast(sender, HtmlDocument).GetElementFromPoint(e.ClientMousePosition).GetAttribute("id")
End Sub

Private Sub buildBrowser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    myHTMLDocument = myBrowser.Document
    AddHandler myHTMLDocument.MouseOver, AddressOf Document_MouseOver
End Sub
End Class

Running this causes an exception, when the code gets to AddHandler myHTMLDocument.MouseOver, AddressOf Document_MouseOver saying that a "Null Reference Exception was unhandled" - specifically "An unhandled exception of type 'System.NullReferenceException' occurred in myProgram.exe"

I've not come across this type of error when adding a Handler to an object before before - so any help would be very gratefully received.

Thanks,

Paul.

paul frith
  • 471
  • 1
  • 4
  • 14
  • 4
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Luke Sep 07 '16 at 13:00
  • http://stackoverflow.com/a/4660186/5686960 is what you are looking for. – Luke Sep 07 '16 at 13:02
  • 1
    What is the value of `myBrowser.Document` when the exception occurs? Since it is occurring in the Load event, is it possible that there is no document loaded yet? – Chris Dunaway Sep 07 '16 at 13:10
  • Just realised how my question read and have therefore slightly ammended my last comment - I am already aware of what a Null Reference is - I am having problems specifically understanding why I am getting a Null Reference in this instance... – paul frith Sep 07 '16 at 13:13
  • Chris, You are correct - I should have a default document loaded and I haven't thank you. If you put this as an answer, I will happily give you a tick! – paul frith Sep 07 '16 at 13:14
  • Well, what is null (Nothing)? Either `myHTMLDocument` is null or `myBrowser` is null. – Chris Dunaway Sep 07 '16 at 13:16

0 Answers0