Click here to Skip to main content
Click here to Skip to main content

Advanced Bookmark Control

, 31 Dec 2009 CC (ASA 2.5)
Rate this:
Please Sign up or sign in to vote.
Make Bookmarks (or Favorites) for your web browser

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

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.

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 

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).

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

Share

About the Author

VisualBas08
Software Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralA good Bookmark Control Pinmemberyyhotspring30-Dec-09 18:29 
GeneralSPAM! Pinmembersam.hill30-Dec-09 19:52 
GeneralRe: SPAM! PinmemberiSurf Elite Company31-Dec-09 8:57 
GeneralRe: SPAM! Pinmembersam.hill31-Dec-09 9:01 
GeneralRe: SPAM! PinmemberiSurf Elite Company31-Dec-09 11:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 31 Dec 2009
Article Copyright 2009 by VisualBas08
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid