简体   繁体   English

在代码生成的选项卡中将goback()与多个代码生成的网络浏览器一起使用

[英]Using goback() with multiple code generated webbrowser in code generated Tabs

I'm fairly new to vb.net, I know the basics. 我是vb.net的新手,我知道一些基础知识。 I have this code to generate multiple Tabs for each pdf file i have inside a directory on my c: Drive. 我有这段代码为c:驱动器上的目录中的每个pdf文件生成多个选项卡。 The code also generates a webbrowser for each Tab and assign the correct pdf to each webbrowser.Some pdf's have links to other pdf's.When i click on these links the pdf opens inside the parent pdf webbrowser. 该代码还会为每个选项卡生成一个Web浏览器,并为每个Web浏览器分配正确的pdf。某些pdf具有指向其他pdf的链接。当我单击这些链接时,该pdf将在父pdf Web浏览器中打开。 I have created a button to use theweb.goback() command, but it does nothing. 我已经创建了一个使用theweb.goback()命令的按钮,但是它什么也没做。 I would like to view the linked pdf and then click back and go back to the main pdf. 我想查看链接的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

I have updated my code with the following. 我已经用以下代码更新了我的代码。 It works perfect. 完美的作品。 Only problem is when i click "Button1", it refreshes and automatically go back to the first tab even thou i was busy on the 15th Tab. 唯一的问题是,当我单击“ 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

You have defined the variable theweb twice so I'm surprised this even compiles. 您已经两次定义了变量theweb ,所以我对此编译感到惊讶。

Even if it does, the reference to theweb you are using to in the button click will be the last webbrowser control that you created. 即使这样做,在单击按钮时对您正在使用的theweb的引用也将是您创建的最后一个theweb浏览器控件。

You need to access the actual browser control on the current tab page and call GoBack on that. 您需要访问当前选项卡页面上的实际浏览器控件,然后在其上调用GoBack。

You should definitely use another browser than the preinstalled browser of .Net Framework, because internal this is Internet Explorer. 绝对应该使用与预安装的.Net Framework浏览器不同的浏览器,因为内部这是Internet Explorer。

Take a look at Google Chrome engine. 看看Google Chrome引擎。

The problem in your code is, that you create a class global instance of your webbrowser, see 代码中的问题是,您创建了Web浏览器的类全局实例,请参见

Public Class Form1
Dim theweb As New WebBrowser

and you do not operate with this browser at all. 并且根本不使用此浏览器。 So this cannot work. 所以这行不通。 You have a class global instance which you do not use. 您有一个不使用的类全局实例。

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM