open in a new window 仅适用于 webbowser 的第一个 TAB

open in a new window only works on the first TAB at webbowser

我写这段代码是为了在新的 TAB 中打开链接,如果我右键单击链接并选择在新的 window 中打开,就可以了!

我的问题是:如果我在第一个 TAB 中它可以工作,但在第二个 TAB 中不起作用。

我应该添加什么才能使此代码适用于整个选项卡?有什么提示吗?

Private Sub WebBrowser1_NewWindow1(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
   e.Cancel = True

    Dim thiselement As HtmlElement = WebBrowser1.Document.ActiveElement
    Dim Browser As New WebBrowser
    Dim targeturl As String = thiselement.GetAttribute("href")
    With Browser
        .Navigate(targeturl)
        .Dock = DockStyle.Fill
    End With

    TabControl1.TabPages.Add(targeturl)
    TabControl1.SelectTab(int)
    TabControl1.SelectedTab.Controls.Add(Browser)
    AddHandler Browser.ProgressChanged, AddressOf Loading
    AddHandler Browser.DocumentCompleted, AddressOf Done
    int = int + 1

End Sub

您没有为 NewWindow1 事件添加处理程序:

Private Sub WebBrowser1_NewWindow1(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles WebBrowser1.NewWindow
    e.Cancel = True

    Dim thiselement As HtmlElement = WebBrowser1.Document.ActiveElement
    Dim Browser As New WebBrowser
    Dim targeturl As String = thiselement.GetAttribute("href")
    With Browser
        .Navigate(targeturl)
        .Dock = DockStyle.Fill
    End With

    TabControl1.TabPages.Add(targeturl)
    TabControl1.SelectTab(TabControl1.TabPages.Count - 1)
    TabControl1.SelectedTab.Controls.Add(Browser)
    AddHandler Browser.ProgressChanged, AddressOf Loading
    AddHandler Browser.DocumentCompleted, AddressOf Done

    'You missed this line.
    AddHandler Browser.NewWindow, AddressOf WebBrowser1_NewWindow1
End Sub

我还用 TabControl1.TabPages.Count - 1 替换了 int 变量,它选择了最后一个选项卡(这是新选项卡),因为 int 变量可能会在其他事件期间发生变化。因此,我建议您使用 TabControl1.TabPages.CountTabControl1.SelectedTab(或者甚至 TabControl1.SelectedIndex)来实现这些方法。