「myBrowser」という名前の webBrowser オブジェクトを含むフォームがあります。このブラウザーで Web ページを開くと、マウスがホバーしている要素の 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
「Null 参照例外が処理されませんでした」、具体的には「タイプ 'System.NullReferenceException' の未処理の例外が myProgram.exe で発生しました」と言うようになると、例外が発生します。
以前にハンドラをオブジェクトに追加するときにこの種のエラーに遭遇したことはありません。そのため、どんな助けも非常に感謝しています。
ありがとう、
ポール。