Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

Advanced Bookmark Control

3.67/5 (2 votes)
31 Dec 2009CC (ASA 2.5)2 min read 41.5K   542  
Make Bookmarks (or Favorites) for your web browser
Image 1

Introduction  

As many Visual Studio programmers know, Microsoft has released an Internet Explorer-based Web Browser control with Visual Studio 2005 and more current versions.

The WebBrowser control it provided is a managed wrapper around the IE control found in earlier versions of Visual Studio (SHDocVW.DLL) that shows some functions that were a little easy to work with, whereas at the same time providing some functions that weren't exactly that easy to get at without hundreds of lines of coding and research.

The object of this article and the provided source code is to show you how to work with the provided WebBrowser control and use it to make bookmarks with your on-going Web Browser application.

Adding Bookmarks  

Image of the Add Bookmark Form

VB.NET
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

In this VB.NET code snippet, you see that by clicking the "Bookmark this Page" DropDownButton, you add the current URL of the Web Browser to both the My.Settings.Bookmarks (System.Collections.Specialized.StringCollection type) and the DropDownItems for the Bookmarks. The code was from "ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs", defining the "e" DIM.

VB.NET
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 

Here you see how you add a custom bookmark, one that's not the current URL. The same method is used for adding the bookmark; the only difference is that this time we use an InputBox function that returns the string typed. 

Removing Bookmarks 

Image 3
VB.NET
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 

In the Edit Bookmarks form, there is the option of removing installed bookmarks (a ListBox contains current ones).  Using the "RemoveAt" method, we can accurately get rid of the selected bookmark, or ListBox1.SelectedIndex. When removing from a list of DropDownItems or ListBox, you have to properly see which index, NOT item, you want to remove (hint: the index value is always one less than the count value).

VB.NET
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 

In another option in the same form, you can remove all bookmarks, whose code is a little bit more textually complicated than the Remove code.  If the user replies to the message box as a "yes," the code will clear the contents of the My.Settings.Bookmarks file and the ListBox1.Items.  Here, we also use a "Do...Loop Until <condition>" statement.  In this case, it makes the DropDownButton's DropDownItems remove the 7th item (remember Count = Index + 1) until the number of remaining items is 6.  It is basically self-explanatory.

History

This is currently the first version of the Advanced Bookmark Control.

Another one will be released as an Intermediate level article.  

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-ShareAlike 2.5 License