2016-09-29 109 views
1

在一个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 

当我下面的代码添加到通过web浏览器文件属性验证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 
+0

谢谢Zagger。这个和其他的例子工作!!!!。对不起.. Zaggler。 –

回答

1

更改所有AndAndAlso ...至于现在你还在试图评估的逻辑。

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

+0

谢谢Zaggler。这个和其他的例子工作!!!!。 –