VB.net Web浏览器 - 添加书签

时间:2013-10-16 15:41:11

标签: vb.net visual-studio-2010 browser

我有一个网络浏览器,我试图保持最小化以保持其快速运行(我有firefox gecko浏览器引擎,即使这对我认为这个问题并不重要。)但是有一个我要添加的东西,那就是书签。现在我有大量凌乱的代码,但每次点击书签按钮时我都无法创建新的工具条按钮。所以我做的是添加了适当的设置和6个工具条按钮。现在这限制了我6个书签。这真的很糟糕。我的代码在这里:

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

度过愉快的一天。

1 个答案:

答案 0 :(得分:0)

创建一个名为favorite的类和一个名为favorites的收藏夹类型为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() - 通过&#34; |&#34;分割每个设置。标题和网址分开的

Class Favorite() - 允许您添加一个拆分按钮,用于将链接的浏览器导航到它的myurl变量。

希望这会有所帮助。抱歉,如果它有点粗糙,我不得不从记忆中回忆起它。