【发布时间】:2016-11-19 22:35:18
【问题描述】:
我正在尝试使用 VB.Net 中的 Webbrowser 组件构建我的第一个 HTML UI。我在 Microsoft 网站上找到了这个代码示例
https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.document(v=vs.110).aspx:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) _
Handles Me.Load
WebBrowser1.DocumentText =
"<html><body>Please enter your name:<br/>" &
"<input type='text' name='userName'/><br/>" &
"<a href='http://www.microsoft.com'>continue</a>" &
"</body></html>"
End Sub
Private Sub webBrowser1_Navigating(
ByVal sender As Object, ByVal e As WebBrowserNavigatingEventArgs) _
Handles WebBrowser1.Navigating
Dim document As System.Windows.Forms.HtmlDocument =
WebBrowser1.Document
If document IsNot Nothing And
document.All("userName") IsNot Nothing And
String.IsNullOrEmpty(
document.All("userName").GetAttribute("value")) Then
e.Cancel = True
MsgBox("You must enter your name before you can navigate to " &
e.Url.ToString())
End If
End Sub
当我把它放到测试中时,大部分时间都会在这部分代码中抛出异常'System.NullReferenceException':
If document IsNot Nothing And
document.All("userName") IsNot Nothing And
String.IsNullOrEmpty(
document.All("userName").GetAttribute("value")) Then
有时它会起作用,但大多数情况下它根本不起作用。知道如何解决这个问题吗?我对 .Net 平台非常陌生,如果有任何拼写错误,我深表歉意。任何帮助表示赞赏。
【问题讨论】:
-
将您的
And更改为AndAlso...这是短路...
标签: vb.net webbrowser-control nullreferenceexception