如果网站未打开,则向用户显示消息

时间:2013-05-27 15:48:52

标签: vb.net visual-studio visual-studio-2008

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

  

“网站无效。请与管理员联系。”

我该怎么做?这就是我所拥有的

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

1 个答案:

答案 0 :(得分:-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,并在Webbrowser完全加载时简单地比较“endstate”URL。如果您希望将其推广到任何网站,您可以从此链接about page event handling for navigation errors.

中找到大部分信息