65.9K
CodeProject 正在变化。 阅读更多。
Home

高级书签控件

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.67/5 (2投票s)

2009 年 12 月 30 日

CC (ASA 2.5)

2分钟阅读

viewsIcon

42752

downloadIcon

564

为您的网页浏览器制作书签(或收藏夹)

介绍  

正如许多 Visual Studio 程序员所知,微软在 Visual Studio 2005 及更高版本中发布了一个基于 Internet Explorer 的 Web 浏览器控件。

它提供的 WebBrowser 控件是早期 Visual Studio 版本中 (SHDocVW.DLL) 找到的 IE 控件的托管包装器,它显示了一些易于使用的函数,同时也提供了一些没有经过数百行代码和研究就难以获取的函数。

本文和提供的源代码的目的是向您展示如何使用提供的 WebBrowser 控件,并用它来为您的正在运行的 Web 浏览器应用程序创建书签。

添加书签

Image of the Add Bookmark Form

Select Case e.ClickedItem.Name
    Case "BookmarkThisPageToolStripMenuItem"
        Try
           My.Settings.Bookmarks.Add(WebBrowser1.Url.AbsoluteUri)
           BookmarksDropDownButton.DropDownItems.Add(WebBrowser1.Url.AbsoluteUri)
           My.Settings.Save() : My.Settings.Reload()
        Catch ex As Exception
           MsgBox(ex.Message)
         End Try

在这个 VB.NET 代码片段中,您可以看到通过单击“为此页添加书签”DropDownButton,您可以将 Web 浏览器的当前 URL 添加到 My.Settings.Bookmarks System.Collections.Specialized.StringCollection 类型)和书签的 DropDownItems 中。 代码来自“ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs”,定义了“eDIM

Case "AddBookmarkToolStripMenuItem"
    Dim Bookmark As String = InputBox("Location of WebSite", _
    "Add Custom Bookmark", "http://www.")
    If Not Bookmark = "" Then
        My.Settings.Bookmarks.Add(Bookmark)
        BookmarksDropDownButton.DropDownItems.Add(Bookmark)
        My.Settings.Save() : My.Settings.Reload()
    End If 

在这里,您可以看到如何添加自定义书签,即不是当前 URL 的书签。 添加书签的方法相同;唯一的区别在于,这次我们使用一个 InputBox 函数,该函数返回输入的字符串。

删除书签

If Not (ListBox1.Items.Count = 0) And Not _ 
       (ListBox1.SelectedIndices.Count = 0) Then
     Dim SI As Integer = ListBox1.SelectedIndex
     ListBox1.Items.RemoveAt(SI)
     My.Settings.Bookmarks.RemoveAt(SI)
     FrmMain.BookmarksDropDownButton.DropDownItems.RemoveAt(SI + 6)
     My.Settings.Save() : My.Settings.Reload()
 End If 

在编辑书签表单中,可以选择删除已安装的书签(ListBox 包含当前的书签)。 使用“RemoveAt”方法,我们可以准确地删除所选书签,即 ListBox1.SelectedIndex。 在从 DropDownItems ListBox 中删除时,您必须正确查看要删除的索引,而不是项目(提示:索引值始终比计数值小 1)。

If Not (ListBox1.Items.Count = 0) Then
    If MessageBox.Show("Are you sure you want to remove all bookmarks?", _
                       "Clear Bookmarks", MessageBoxButtons.YesNo, _
                        MessageBoxIcon.Question) _
                        = Windows.Forms.DialogResult.Yes Then
         Do : FrmMain.BookmarksDropDownButton.DropDownItems.RemoveAt(6)
         Loop Until (FrmMain.BookmarksDropDownButton.DropDownItems.Count = 6)
         ListBox1.Items.Clear()
         My.Settings.Bookmarks.Clear()
         My.Settings.Save() : My.Settings.Reload()
     End If
End If 

在同一表单的另一个选项中,您可以删除所有书签,其代码比删除代码稍微复杂一些。 如果用户在消息框中回复“是”,代码将清除 My.Settings.Bookmarks 文件和 ListBox1.Items 的内容。 在这里,我们还使用“Do...Loop Until <condition>”语句。 在这种情况下,它将删除 DropDownButtonDropDownItems 中的第 7 个项目(记住 Count = Index + 1),直到剩余的项目数为 6。 这基本上是不言自明的。

历史

这是高级书签控件的第一个版本。

另一个版本将作为中级文章发布。

高级书签控件 - CodeProject - 代码之家
© . All rights reserved.