简体   繁体   中英

Using goback() with multiple code generated webbrowser in code generated Tabs

I'm fairly new to vb.net, I know the basics. I have this code to generate multiple Tabs for each pdf file i have inside a directory on my c: Drive. 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. I have created a button to use theweb.goback() command, but it does nothing. I would like to view the linked pdf and then click back and go back to the main 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.

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.

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.

You need to access the actual browser control on the current tab page and call GoBack on that.

You should definitely use another browser than the preinstalled browser of .Net Framework, because internal this is Internet Explorer.

Take a look at Google Chrome engine.

The problem in your code is, that you create a class global instance of your webbrowser, see

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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