加载webbrowser文档后如何做某事

时间:2013-09-05 18:31:46

标签: .net vb.net webbrowser-control

只有在vb.net webbrowser中完全加载了特定的URL时,如何在子例程中运行一些东西。

e.g。

sub button click

webbrowsernavigate to whatever

(This is what I need)if document has loaded statement
do stuff

end sub

谢谢

1 个答案:

答案 0 :(得分:1)

WebBrowser类有一个DocumentCompleted事件可以绑定到:

  

WebBrowser控件完成加载文档时发生。

MSDN文章有一个示例,演示了如何有效地使用此事件:

Private Sub PrintHelpPage()

    ' Create a WebBrowser instance.  
    Dim webBrowserForPrinting As New WebBrowser()

    ' Add an event handler that prints the document after it loads. 
    AddHandler webBrowserForPrinting.DocumentCompleted, New _
        WebBrowserDocumentCompletedEventHandler(AddressOf PrintDocument)

    ' Set the Url property to load the document.
    webBrowserForPrinting.Url = New Uri("\\myshare\help.html")

End Sub 

Private Sub PrintDocument(ByVal sender As Object, _
    ByVal e As WebBrowserDocumentCompletedEventArgs)

    Dim webBrowserForPrinting As WebBrowser = CType(sender, WebBrowser)

    ' Print the document now that it is fully loaded.
    webBrowserForPrinting.Print()
    MessageBox.Show("print")

    ' Dispose the WebBrowser now that the task is complete. 
    webBrowserForPrinting.Dispose()

End Sub