我有一个带有名为“myBrowser”的 webBrowser 对象的表单。当我在此浏览器中打开网页时,我想获取鼠标悬停在上面的任何元素的 id,并将其显示在名为“txtProperties”的文本框中。
我尝试使用以下代码来实现这一点:
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
运行此程序会导致异常,当代码开始AddHandler myHTMLDocument.MouseOver, AddressOf Document_MouseOver
说“未处理空引用异常” - 特别是“myProgram.exe 中发生'System.NullReferenceException' 类型的未处理异常”
在之前向对象添加处理程序时,我没有遇到过这种类型的错误 - 所以任何帮助都将非常感激。
谢谢,
保罗。