在代码生成的Tabs中使用goback()与多个代码生成webbrowser

时间:2016-01-20 13:15:58

标签: .net vb.net pdf webbrowser-control controls

我对vb.net很新,我知道基础知识。我有这个代码为我的c:Drive上的目录中的每个pdf文件生成多个选项卡。该代码还为每个Tab生成一个webbrowser,并为每个webbrowser分配正确的pdf。一些pdf有其他pdf的链接。当我点击这些链接时,pdf在父pdf webbrowser中打开。我创建了一个使用theweb.goback()命令的按钮,但它什么也没做。我想查看链接的pdf,然后单击返回并返回主pdf。

Imports System.IO
Public Class Form1
    Dim theweb As New WebBrowser
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each A As String In System.IO.Directory.GetFiles("N:\Drawing Office\Standards Appplication\PDF")
            Dim A2 As String = System.IO.Path.GetFileNameWithoutExtension(A)
            Dim myTabPage As New TabPage()
            myTabPage.Text = A2
            TabControl1.TabPages.Add(myTabPage)
            Dim theweb As New WebBrowser
            Dim Url As String = A
            theweb.GoHome()
            theweb.Parent = myTabPage
            theweb.Visible = True
            theweb.Dock = DockStyle.Fill
            theweb.Navigate(Url)
        Next

    End Sub



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        theweb.GoBack()
    End Sub
End Class

我已使用以下内容更新了我的代码。它完美无缺。唯一的问题是当我点击“Button1”时,它会刷新并自动返回到第一个标签,即使你在第15个标签页上忙碌。

Imports System.IO
Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each A As String In System.IO.Directory.GetFiles("N:\Drawing Office\Standards Appplication\PDF")
            Dim A2 As String = System.IO.Path.GetFileNameWithoutExtension(A)
            Dim myTabPage As New TabPage()
            myTabPage.Text = A2
            TabControl1.TabPages.Add(myTabPage)
            Dim theweb As New WebBrowser
            Dim Url As String = A
            theweb.GoHome()
            theweb.Parent = myTabPage
            theweb.Visible = True
            theweb.Dock = DockStyle.Fill
            theweb.Navigate(Url)
        Next

    End Sub



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        testIt()
    End Sub
    Private Sub testIt()
        TabControl1.TabPages.Clear()

        For Each A As String In System.IO.Directory.GetFiles("N:\Drawing Office\Standards Appplication\PDF")
            Dim A2 As String = System.IO.Path.GetFileNameWithoutExtension(A)
            Dim myTabPage As New TabPage()
            myTabPage.Text = A2
            TabControl1.TabPages.Add(myTabPage)
            Dim theweb As New WebBrowser
            Dim Url As String = A
            theweb.GoHome()
            theweb.Parent = myTabPage
            theweb.Visible = True
            theweb.Dock = DockStyle.Fill
            theweb.Navigate(Url)
        Next
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

您已经将变量theweb定义了两次,所以即使编译我也感到惊讶。

即使它确实如此,您在按钮单击中使用的theweb引用也将是您创建的最后一个webbrowser控件。

您需要访问当前标签页上的实际浏览器控件并在其上调用GoBack。

答案 1 :(得分:-1)

您绝对应该使用除预装的.Net Framework浏览器之外的其他浏览器,因为内部这是Internet Explorer。

查看Google Chrome引擎。

您的代码中的问题是,您创建了一个Web浏览器的类全局实例,请参阅

Public Class Form1
Dim theweb As New WebBrowser

并且您根本不使用此浏览器。所以这不行。 您有一个不使用的类全局实例。

试试这个:

Public Class Form1
Private Sub TestIt()
Dim theweb As New WebBrowser
    For Each file As String In Directory.GetFiles("N:\Drawing Office\Standards Appplication\PDF")
        Dim A2 As String = Path.GetFileNameWithoutExtension(file)

        Dim myTabPage As New TabPage()
        myTabPage.Text = A2
        TabControl1.TabPages.Add(myTabPage)

        With theweb
            .GoHome()
            .Parent = myTabPage
            .Visible = True
            .Dock = DockStyle.Fill
            .Navigate(file)
        End With

    Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TestIt()
End Sub
End Class