当不满足条件时,评估条件继续

时间:2016-09-29 23:44:34

标签: vb.net visual-studio webbrowser-control

在Visual Studio窗体的WebBrowser控件中,此代码可以正常工作:

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

    WebBrowser1.DocumentText =
    "<html><body>Search in Google:<br/>" &
    "<form method='get' action='http://www.google.com/search'>" &
    "<input type='text' name='as_q'/><br/>" &
    "<input type='submit' value='Search'/>" &
    "</form></body></html>"

End Sub

当我将以下代码添加到通过WebBrowser Document属性验证HTML表单时,请收到以下错误消息:对象引用未设置为对象的实例。

 Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating

    Dim document = WebBrowser1.Document
    If document IsNot Nothing And
    document.All("as_q") IsNot Nothing And
    String.IsNullOrEmpty(
    document.All("as_q").GetAttribute("value")) Then
        e.Cancel = True
        MsgBox("Please enter a search term.")
    End If

End Sub

1 个答案:

答案 0 :(得分:1)

将您的所有And更改为AndAlso ...截至目前,您仍在尝试评估逻辑。

当您使用AndAlso 时,它会短路并在不符合条件时跳出。