2016-01-20 40 views
0

我是相當新的vb.net,我知道的基本知識。我有這個代碼來爲我的c:Drive上的目錄中的每個pdf文件生成多個Tabs。該代碼還爲每個Tab生成一個webbrowser,併爲每個webbrowser分配正確的pdf。有些pdf與其他pdf的鏈接。當我點擊這些鏈接時,pdf將在父頁面的pdf瀏覽器中打開。我創建了一個使用web.goback()命令的按鈕,但它什麼都不做。我想查看鏈接的pdf,然後點擊返回到主要pdf。使用GoBack的(),在代碼中的多個代碼產生的網頁瀏覽器選項卡

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 

回答

-1

你一定要使用其他瀏覽器比.NET Framework的預裝瀏覽器,因爲內部這是IE瀏覽器。

看看谷歌瀏覽器引擎。

在你的代碼的問題是,你創建你的網頁瀏覽器的一類全局實例,見

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 
+0

試過了。不起作用。 –

0

您已經定義的變量theweb兩次,我很驚訝這甚至編譯。

即使是這樣,到theweb參考你在點擊按鈕用來將是最後創建的WebBrowser控件。

你需要訪問當前選項卡頁面上實際瀏覽器控件,並調用GoBack的這一點。

+0

現在我明白了。我刪除了Form_load事件上方的變量。我如何訪問當前標籤頁上的實際瀏覽器控件?我搜索並嘗試了所有能力。 –

相關問題