2013-10-16 117 views
0

我有一個Web瀏覽器,我試圖保持最小限度,以保持它的運行速度(我有firefox gecko瀏覽器引擎,儘管這對於這個問題並不重要,我認爲),但我想添加一件事,那就是書籤。現在我有一大堆亂七八糟的代碼,但是我每次點擊書籤按鈕時都無法創建新的工具條按鈕。所以我所做的是我添加了適當的設置和6個工具條按鈕。現在這限制了我6個書籤。這真的很糟糕。我的代碼在這裏:VB.net Web瀏覽器 - 添加書籤

Imports System.IO 

Public Class tabForm 
    Dim ico As Image = Nothing 
    Private Sub goBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goBack.Click 
     webBrowser.GoBack() 
    End Sub 

Private Sub goForward_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles goForward.Click 
    webBrowser.GoForward() 
End Sub 

Private Sub Navigate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Navigate.Click 
    If urlBox.Text = "yt" Then 
     webBrowser.Navigate("http://www.youtube.com") 
    ElseIf urlBox.Text = "fb" Then 
     webBrowser.Navigate("http://www.facebook.com") 
    ElseIf urlBox.Text = "gm" Then 
     webBrowser.Navigate("http://www.gmail.com") 
    ElseIf urlBox.Text = "go" Then 
     webBrowser.Navigate("http://www.google.com") 
    Else 
     webBrowser.Navigate(urlBox.Text) 
    End If 
End Sub 

Private Sub webBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.EventArgs) Handles webBrowser.DocumentCompleted 
    Me.Text = webBrowser.DocumentTitle 
    geticon() 
End Sub 

Private Sub webBrowser_Navigated(ByVal sender As System.Object, ByVal e As Skybound.Gecko.GeckoNavigatedEventArgs) Handles webBrowser.Navigated 
    Try 
     urlBox.Text = webBrowser.Url.ToString 

    Catch ex As Exception 

    End Try 
End Sub 

Private Sub urlBox_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles urlBox.KeyDown 
    Try 
     If e.KeyCode = Keys.Enter Then 
      If urlBox.Text = "yt" Then 
       webBrowser.Navigate("http://www.youtube.com") 
      ElseIf urlBox.Text = "fb" Then 
       webBrowser.Navigate("http://www.facebook.com") 
      ElseIf urlBox.Text = "gm" Then 
       webBrowser.Navigate("http://www.gmail.com") 
      ElseIf urlBox.Text = "go" Then 
       webBrowser.Navigate("http://www.google.com") 
      Else 
       webBrowser.Navigate(urlBox.Text) 
      End If 
      e.SuppressKeyPress = True 
     End If 
    Catch ex As Exception 

    End Try 
End Sub 

Private Sub geticon() 
    Try 
     Dim url As Uri = New Uri(webBrowser.Url.ToString) 

     If url.HostNameType = UriHostNameType.Dns Then 

      ' Get the URL of the favicon 
      ' url.Host will return such string as www.google.com 
      Dim iconURL = "http://" & url.Host & "/favicon.ico" 

      ' Download the favicon 
      Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(iconURL) 
      Dim response As System.Net.HttpWebResponse = request.GetResponse() 
      Dim stream As System.IO.Stream = response.GetResponseStream() 
      Dim favicon = Image.FromStream(stream) 

      ' Display the favicon on ToolStripLabel1 
      Me.favicon.Image = favicon 
     End If 
    Catch ex As Exception 
     Me.favicon.Image = Nothing 
    End Try 
End Sub 

Private Sub favicon_timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles favicon_timer.Tick 
    Try 

    Catch ex As Exception 

    End Try 
End Sub 

Private Sub Reload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Reload.Click 
    webBrowser.Reload() 
End Sub 

Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click 
    webBrowser.Navigate(My.Settings.mark1) 
End Sub 

Private Sub ToolStripButton6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton6.Click 
    webBrowser.Navigate(My.Settings.mark6) 
End Sub 

Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click 
    webBrowser.Navigate(My.Settings.mark5) 
End Sub 

Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click 
    webBrowser.Navigate(My.Settings.mark4) 
End Sub 

Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click 
    webBrowser.Navigate(My.Settings.mark3) 
End Sub 

Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click 
    webBrowser.Navigate(My.Settings.mark2) 
End Sub 

Private Sub Fav_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 

End Sub 

Private Sub ToolStripButton7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton7.Click 
    If ToolStripButton1.Text = webBrowser.DocumentTitle Then 
     ToolStripButton1.Text = "" 
     My.Settings.mark1 = "" 
     My.Settings.mark11 = "" 
     ToolStripButton1.Visible = False 
    End If 
    If ToolStripButton2.Text = webBrowser.DocumentTitle Then 
     ToolStripButton2.Text = "" 
     My.Settings.mark2 = "" 
     My.Settings.mark22 = "" 
     ToolStripButton2.Visible = False 
    End If 
    If ToolStripButton3.Text = webBrowser.DocumentTitle Then 
     ToolStripButton3.Text = "" 
     My.Settings.mark3 = "" 
     My.Settings.mark33 = "" 
     ToolStripButton3.Visible = False 
    End If 
    If ToolStripButton4.Text = webBrowser.DocumentTitle Then 
     ToolStripButton4.Text = "" 
     My.Settings.mark4 = "" 
     My.Settings.mark44 = "" 
     ToolStripButton4.Visible = False 
    End If 
    If ToolStripButton5.Text = webBrowser.DocumentTitle Then 
     ToolStripButton5.Text = "" 
     My.Settings.mark5 = "" 
     My.Settings.mark55 = "" 
     ToolStripButton5.Visible = False 
    End If 
    If ToolStripButton6.Text = webBrowser.DocumentTitle Then 
     ToolStripButton6.Text = "" 
     My.Settings.mark6 = "" 
     My.Settings.mark66 = "" 
     ToolStripButton6.Visible = False 
    End If 
End Sub 

Private Sub ToolStripButton8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton8.Click 
    If ToolStripButton1.Text = "" Then 
     ToolStripButton1.Text = webBrowser.DocumentTitle 
     My.Settings.mark1 = webBrowser.Url.ToString 
     My.Settings.mark11 = webBrowser.DocumentTitle 
     ToolStripButton1.Visible = True 
    ElseIf ToolStripButton2.Text = "" Then 
     ToolStripButton2.Text = webBrowser.DocumentTitle 
     My.Settings.mark2 = webBrowser.Url.ToString 
     My.Settings.mark22 = webBrowser.DocumentTitle 
     ToolStripButton2.Visible = True 
    ElseIf ToolStripButton3.Text = "" Then 
     ToolStripButton3.Text = webBrowser.DocumentTitle 
     My.Settings.mark3 = webBrowser.Url.ToString 
     My.Settings.mark33 = webBrowser.DocumentTitle 
     ToolStripButton3.Visible = True 
    ElseIf ToolStripButton4.Text = "" Then 
     ToolStripButton4.Text = webBrowser.DocumentTitle 
     My.Settings.mark4 = webBrowser.Url.ToString 
     My.Settings.mark44 = webBrowser.DocumentTitle 
     ToolStripButton4.Visible = True 
    ElseIf ToolStripButton5.Text = "" Then 
     ToolStripButton5.Text = webBrowser.DocumentTitle 
     My.Settings.mark5 = webBrowser.Url.ToString 
     My.Settings.mark55 = webBrowser.DocumentTitle 
     ToolStripButton5.Visible = True 
    ElseIf ToolStripButton6.Text = "" Then 
     ToolStripButton6.Text = webBrowser.DocumentTitle 
     My.Settings.mark6 = webBrowser.Url.ToString 
     My.Settings.mark66 = webBrowser.DocumentTitle 
     ToolStripButton6.Visible = True 
    Else 

    End If 
End Sub 

Private Sub ToolStripButton9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton9.Click 
    Dim newFav As New ToolStripButton 
    newFav.Text = webBrowser.Url.ToString 
    newFav.PerformClick() 
End Sub 

Private Sub favClick() 

End Sub 

Private Sub tabForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    If My.Settings.mark1 <> "" And My.Settings.mark11 <> "" Then 
     ToolStripButton1.Text = My.Settings.mark11 
     ToolStripButton1.Visible = True 
    End If 
    If My.Settings.mark2 <> "" And My.Settings.mark22 <> "" Then 
     ToolStripButton2.Text = My.Settings.mark22 
     ToolStripButton2.Visible = True 
    End If 
    If My.Settings.mark3 <> "" And My.Settings.mark33 <> "" Then 
     ToolStripButton3.Text = My.Settings.mark33 
     ToolStripButton3.Visible = True 
    End If 
    If My.Settings.mark4 <> "" And My.Settings.mark44 <> "" Then 
     ToolStripButton4.Text = My.Settings.mark44 
     ToolStripButton4.Visible = True 
    End If 
    If My.Settings.mark5 <> "" And My.Settings.mark55 <> "" Then 
     ToolStripButton5.Text = My.Settings.mark55 
     ToolStripButton5.Visible = True 
    End If 
    If My.Settings.mark6 <> "" And My.Settings.mark66 <> "" Then 
     ToolStripButton6.Text = My.Settings.mark66 
     ToolStripButton6.Visible = True 
    End If 
    End Sub 
End Class 

就是這樣。但正如我剛纔所說,我只能添加6個書籤。有什麼辦法可以添加無限數量的書籤。我試過這樣的事情:

Private Sub Bookmark() 
    Dim mark As New ToolStripButton 
    mark.DisplayStyle = Text 
    mark.Parent = ToolStrip1 'This didn't work 
End Sub 

但是,如果我確實得到這個工作,我會把它導航到該頁面?請幫忙。

我終於找到了答案。如果有人想知道這裏是鏈接:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/eebcf40a-dec9-41ae-8e8b-3d446cf93322/web-browser-bookmarks-bar

有一個愉快的一天。

回答

0

創建一個類調用喜愛和設置類型system.collections.specialized.stringcollection

public class favorite 
    inherits toolstripsplitbutton 
    public mytitle as string 
    public myurl as string 
    public myimg as image 
    public mybrowser as webbrowser 

    public sub new(title as string, url as string, browser as webbrowser) 
     mybase.new() 

     mytitle = title 
     myurl = url 
     'create a getfavicon function - Google :) 
     myimg = GetFavicon(myurl) 
     mybrowser = browser 
     me.text = mytitle 
     me.tooltiptext = mytitle + " : " + myurl 
     me.image = myimg 
    end sub 

    private sub Favorite_Click(sender as object, e as eventargs) handles me.click 
     browser.navigate(myurl) 
    end sub 

然後在表單代碼添加子

public sub AddFav() handles '>>the add favorite button<<.click 
    my.settings.favorites.add(webbrowser.documenttitle + "|" + webbrowser.url.tostring) 
    my.settings.save() 
end sub 

public sub RefreshFavs() 
    for each fav as string in my.settings.favorites 
     dim favarray as array = fav.split(new char() {"|"c}) 
     dim x as new favorite(favarray(0), favarray(favarray.count - 1, webbrowser) 
     mytoolstrip.items.add(x) 
    next 
end sub 

簡單的解釋的所謂最愛:

AddFav() - 用頁面標題和網址保存收藏夾

RefreshFavs() - 通過「|」分割每個設置標題和網址是分開的

Class Favorite() - 允許您添加一個splitbutton,將鏈接的瀏覽器導航到它的myurl變量。

希望這有助於。對不起,如果它有點粗俗,我不得不從記憶中回憶它。

+0

@Charles這裏是我的答案 – user2155059

+0

謝謝你的幫助。 – Charles