1

I put a webbrowser control in my windows form application and I would like my visual basic.net script to know if a button is clicked inside the webpage that is loaded by the webbrowser control. I suppose there must be a way to do this, so far I know how to access document elements by ID but i dont know how to know if their events are fired. thanks for your help!

Imports Gecko
Public Class Form1
    Public myBrowser As GeckoWebBrowser
    Public Sub New()
        InitializeComponent()
        Gecko.Xpcom.Initialize("c:\xulrunner")
        myBrowser = New GeckoWebBrowser()
        myBrowser.Parent = Me
        myBrowser.Dock = DockStyle.Fill
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myBrowser.Navigate("example.com")
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ' MessageBox.Show(myBrowser.Document.GetElementById("divID").TextContent())
    End Sub
    Private Sub divclick(ByVal sender As Object, ByVal e As EventArgs) Handles myBrowser.Document.GetElementById("divID").Click
        'codes that i need to be run
    End Sub
End Class
4

1 回答 1

2

您说您知道如何通过 ID 获取文档元素。一旦你有了一个HtmlElement对象,你就可以将事件处理程序添加到它的事件中。例如:

AddHandler myHtmlElement.Click, AddressOf MyElementClicked

然后MyElementClicked每次单击网页中的元素时都会调用该方法:

Private Sub MyElementClicked(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
    ' ...
End Sub

通常,您会将事件处理程序添加到DocumentCompletedWeb 浏览器控件的事件处理程序中的 HTML 元素。

于 2013-09-13T15:46:44.720 回答