如何在新窗口窗体中打开动态WebBrowser链接地址?

时间:2013-10-01 05:23:32

标签: vb.net browser webbrowser-control

我在href找到了错误所以请帮帮我

Private Sub WebBrowser1_NewWindow(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
    Dim thiselement As HtmlElement = WebBrowser1.Document.ActiveElement
    Dim targeturl As String = thiselement.GetAttribute("href") 
    e.Cancel = True
    Dim window As New Form1
    window.Show()
    window.WebBrowser1.Navigate(targeturl)
End Sub

在“href”我发现像对象引用这样的错误没有设置为对象的瞬间。 我的代码在vb.net 2010中。

2 个答案:

答案 0 :(得分:0)

WebBrowser1.Document.ActiveElement正在返回Nothing,因为没有有效元素。因此,当您尝试使用targeturl时,会出现此错误:Object reference not set to an instant of object

答案 1 :(得分:0)

处理Navigating事件。例如:

webBrowser1.Navigating += Function(source, args) 
    Dim uriClicked = args.Uri
    ' Create your new form or do whatever you want to do here
End Function