一次刷新多个WebBrowsers

时间:2018-11-02 21:20:43

标签: vb.net

我正在以编程方式将Web浏览器添加到标签页,并将名称增加一个(WebBrowser1,WebBrowser2等)

我希望能够在单击按钮时立即刷新所有浏览器。

我尝试遍历控件并调出WebBrowsers刷新它们,但是还没有运气。我还阅读了有关将WebBrowsers添加到数组中,然后使用该数组刷新它们的信息,但是我不确定如何做到这一点。这是我的将Web浏览器添加到标签页的代码:

Dim newTab As TabPage = New TabPage With {.Text = Path.GetFileName(HTMLReportFile)}
Dim wb As WebBrowser = New WebBrowser With {.Name = "WebBrowser" & (TabControl2.TabPages.Count + 1), .Location = New Point(3, 3), .Width = 725, .Height = 496}

            newTab.Controls.Add(wb)
            wb.Navigate(HTMLReportFile)
            TabControl2.Controls.Add(newTab)

我知道有一个简单的解决方案,但是我很沮丧。预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我同意Jimi的观点,将其全部包裹在List(Of WebBrowser)中使生活变得更加简单。

代码将类似于以下内容:

    Dim wbTabs As New List(Of WebBrowser)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim newTab As TabPage = New TabPage With {.Text = "FileName"}
    Dim wb As WebBrowser = New WebBrowser With {.Name = "WebBrowser" & (TabControl1.TabPages.Count + 1), .Location = New Point(3, 3), .Width = 725, .Height = 496}

    wbTabs.Add(wb)

    newTab.Controls.Add(wb)
    wb.Navigate("http://www.google.com")
    TabControl1.Controls.Add(newTab)
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    For Each WB In wbTabs
        WB.Refresh()
    Next

End Sub

如果出于任何原因真的不想将它们包装到列表中,则可以使用以下内容遍历每个选项卡,找到WebBrowser元素并相应地刷新它们:

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        For Each tPage As TabPage In TabControl1.TabPages
            For Each Cntrl As Control In tPage.Controls
                If Cntrl.GetType Is GetType(WebBrowser) Then
                    Cntrl.Refresh()
                End If
            Next
        Next

    End Sub