1

如果请求的网站未打开(或互联网无法正常工作),我会尝试向用户显示消息。我想显示的信息是

“网站不工作,请联系管理员。”

我怎样才能做到这一点?这就是我所拥有的

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("google.com")

        Dim connectn As Boolean

        If connectn = False Then

            MsgBox("site not working please contact the developer")

        End If
    End Sub

    Private Sub Back_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Back.Click
        WebBrowser1.GoBack()

    End Sub

    Private Sub Forward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Forward.Click
        WebBrowser1.GoForward()
    End Sub


    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        WebBrowser1.Stop()
    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        WebBrowser1.GoHome()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        WebBrowser1.Refresh()
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub

    Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click

    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Application.Exit()

    End Sub

    Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

    End Sub

    Private Sub WebBrowser1_DocumentCompleted_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

    End Sub

End Class
4

1 回答 1

-2

最好不要使用 DocumentCompleted 事件处理程序,因为它会在框架完成时触发,而不一定是整个网页。而是使用以下内容:

 With WebBrowser1

 .Navigate("google.com")

    Do Until Not (.IsBusy)
        Application.DoEvents()
    Loop

    Do Until .ReadyState = WebBrowserReadyState.Complete
        Application.DoEvents()
    Loop


End With

除此之外,如果是针对特定站点,您可以找到正确的 URL,并在 Web 浏览器完全加载时简单地比较“endstate” URL。如果您希望它适用于任何网站,您可以从此链接中找到有关导航错误的页面事件处理的大部分信息。

于 2013-05-27T20:20:22.493 回答