Enrique Bello Enrique Bello - 2 years ago 111
Vb.net Question

Evaluating condition continues when condition is not met

In a WebBrowser control in a Visual Studio Form this code works fine:

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'/>" &

End Sub

When I add the following code to Validating an HTML form through the WebBrowser Document property get this error message : Object reference not set to an instance of an object.

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
document.All("as_q").GetAttribute("value")) Then
e.Cancel = True
MsgBox("Please enter a search term.")
End If

End Sub

Answer Source

Change all your And to AndAlso... As of right now you are still trying to evaluate the logic.

When you use the AndAlso it will short circuit and jump out when a condition is not met.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download