Results 1 to 29 of 29

Thread: Web Browser with favorites

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    31

    Cool Web Browser with favorites

    Hi Everyone,

    I have a list box and can currently display the URL's from my favorites folder in it using the code below:

    ListBox1.Items.Clear()
    Dim favouritesPath As String
    favouritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites)
    ListBox1.Items.AddRange(IO.Directory.GetFiles(favouritesPath))

    Is there a way to show the file names in the favorites folder and when they are doubled clicked on in the list box, them to open their URL in the web browser I'm making?

    Thanks,

    M1LA

    PS. I'm using VB Net

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Web Browser with favorites

    Handle the DoubleClick event of the ListBox, get the SelectedItem, call the Navigate method of your WebBrowser control and pass that item.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    31

    Re: Web Browser with favorites

    Thanks,

    But how would I show the names of the favorites (not the urls) and still be able to use the property of the double click event. Because at the moment they are shown as the URL's not the names given by the user.

    m1la

  4. #4
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    One way to do this would be to create a Class that represents your Favourite, something like this:

    vb Code:
    1. Public Class Favourite
    2.  
    3.     Private _displayText As String
    4.     Public Property DisplayText() As String
    5.         Get
    6.             Return _displayText
    7.         End Get
    8.         Set(ByVal value As String)
    9.             _displayText = value
    10.         End Set
    11.     End Property
    12.  
    13.     Private _url As Uri
    14.     Public Property Url() As Uri
    15.         Get
    16.             Return _url
    17.         End Get
    18.         Set(ByVal value As Uri)
    19.             _url = value
    20.         End Set
    21.     End Property
    22.  
    23.     Public Sub New()
    24.  
    25.     End Sub
    26.  
    27.     Public Sub New(ByVal displayText As String, ByVal url As Uri)
    28.         Me.DisplayText = displayText
    29.         Me.Url = url
    30.     End Sub
    31.  
    32.     Public Overrides Function ToString() As String
    33.         Return Me.DisplayText
    34.     End Function
    35. End Class

    Notice here that I have overriden the default .ToString member of this class. This is what is called when it comes to showing the Text that is displayed within your ListBox.

    When it comes to adding a Favourite to the ListBox, you would do something like the following:

    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim myFavourite As New Favourite("Google", New Uri("http://www.google.com"))
    3.  
    4.         Me.ListBox1.Items.Add(myFavourite)
    5.     End Sub

    Or, if you prefer, you could do it like this:

    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Dim myFavourite As New Favourite()
    3.         myFavourite.DisplayText = "Google"
    4.         myFavourite.Url = New Uri("http://www.google.com")
    5.         Me.ListBox1.Items.Add(myFavourite)
    6.     End Sub

    Here, I am creating an instance of a favourite, with both a friendly name, i.e. Google, and the actual Url to the website.

    Then, when it comes to the DoubleClick event of the ListBox, you would need to extract the instance of your class from the ListBox item, and cast it to the Type of your class so that you can extract the properties, and then navigate to the Url. Something like this:

    vb Code:
    1. Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
    2.         Dim selectedFavourite As Favourite = DirectCast(ListBox1.SelectedItem, Favourite)
    3.         Me.WebBrowser1.Navigate(selectedFavourite.Url)
    4.     End Sub

    Hope that helps!!

    Gary

  5. #5
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    Out of interest, did this work for you? Or are you still having issues?

    Gary

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    31

    Cool Re: Web Browser with favorites

    I'm going to try it at the weekend as I'm busy now.

    Thanks,

    m1la

  7. #7
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    Ah ok, no probs.

    Let me know if you have any questions about it.

    Gary

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    31

    Cool Re: Web Browser with favorites

    Hi,

    How would I call on the class to show my favorites in the list box and how where does your code refer to the users favorite folder to get the favorites from it?

    Thanks,

    m1la

  9. #9
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    Short answer is that it doesn't. What I was showing you was a way to display a class of Favourites in a ListBox as requested. I hadn't given any thought as to how this list would be populated. As you can see from the code:

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim myFavourite As New Favourite("Google", New Uri("http://www.google.com"))
     
            Me.ListBox1.Items.Add(myFavourite)
        End Sub
    This is where a "Favourite" is created and added to the ListBox.

    In your case, you are trying to enumerate all the Internet Explorer shortcuts within the users Favourites folder. This "shortcut" is actually an ini file, which, if you open it up, you will see something like the following:

    Code:
    [DEFAULT]
    BASEURL=http://www.google.ca/
    [InternetShortcut]
    URL=http://www.google.ca/
    Modified=205A372825F4C401B7
    IconFile=http://www.google.ca/favicon.ico
    IconIndex=1
    What you need to do is open this file, and grab the URL information from it. One way to do this is to make a PInvoke call to here:

    http://www.pinvoke.net/default.aspx/...eprofilestring

    Which will do the work for you.

    One way to get what you want is the following (I am not saying that this is the best way, as I only spent a couple minutes knocking it together:

    Code:
        <DllImport("kernel32.dll", SetLastError:=True)> _
       Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, _
                               ByVal lpKeyName As String, _
                               ByVal lpDefault As String, _
                               ByVal lpReturnedString As StringBuilder, _
                               ByVal nSize As Integer, _
                               ByVal lpFileName As String) As Integer
        End Function
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ListBox1.Items.Clear()
    
            Dim di As DirectoryInfo = New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites))
            Dim res As Integer
            Dim sb As StringBuilder
    
            For Each fileinfo As FileInfo In di.GetFiles()
                sb = New StringBuilder(500)
                res = GetPrivateProfileString("InternetShortcut", "URL", "", sb, sb.Capacity, fileinfo.FullName)
                If res > 0 Then
                    Dim myFav As New Favourite()
                    myFav.DisplayText = Path.GetFileNameWithoutExtension(fileinfo.FullName)
                    myFav.Url = New Uri(sb.ToString())
                    ListBox1.Items.Add(myFav)
                End If
            Next
    
        End Sub
    This doesn't take into consideration that the user has folders within their Favourites folder to group shortcuts together, so you may want to think about recursing through the folders to make sure that you get them all.

    Hope that helps!!

    Gary

  10. #10
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    Did this work for you?

    Gary

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    31

    Smile Re: Web Browser with favorites

    Hi,

    It works!!!

    Thanks,

    Do you know how I could add favourites?

    Thanks again,

    m1la

  12. #12
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    Glad to hear that it worked for you. You would need to write out a file to the file system with a similar format to the ones that already exists.

    I am on my phone just now, so can't knock up a sample, but I will try and get one tomorrow.

    Gary

  13. #13
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    Ok, here is a one way that you can do this (this assumes that you have two textboxes, one for the name you want to give to the shortcut, and another for the Url of the favourite):

    vb Code:
    1. <DllImport("kernel32.dll", SetLastError:=True)> _
    2.    Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, _
    3.                            ByVal lpKeyName As String, _
    4.                            ByVal lpDefault As String, _
    5.                            ByVal lpReturnedString As StringBuilder, _
    6.                            ByVal nSize As Integer, _
    7.                            ByVal lpFileName As String) As Integer
    8.     End Function
    9.  
    10.     <DllImport("kernel32.dll", SetLastError:=True)> _
    11.     Private Shared Function WritePrivateProfileString(ByVal lpAppName As String, _
    12.                                                       ByVal lpKeyName As String, _
    13.                                                       ByVal lpString As String, _
    14.                                                       ByVal lpFileName As String) As Integer
    15.     End Function
    16.  
    17.  
    18.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    19.         ListBox1.Items.Clear()
    20.  
    21.         Dim di As DirectoryInfo = New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites))
    22.         Dim res As Integer
    23.         Dim sb As StringBuilder
    24.  
    25.         For Each fileinfo As FileInfo In di.GetFiles()
    26.             sb = New StringBuilder(500)
    27.             res = GetPrivateProfileString("InternetShortcut", "URL", "", sb, sb.Capacity, fileinfo.FullName)
    28.             If res > 0 Then
    29.                 Dim myFav As New Favourite()
    30.                 myFav.DisplayText = Path.GetFileNameWithoutExtension(fileinfo.FullName)
    31.                 myFav.Url = New Uri(sb.ToString())
    32.                 ListBox1.Items.Add(myFav)
    33.             End If
    34.         Next
    35.  
    36.     End Sub
    37.  
    38.     Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
    39.         Dim selectedFavourite As Favourite = DirectCast(ListBox1.SelectedItem, Favourite)
    40.         Me.WebBrowser1.Navigate(selectedFavourite.Url)
    41.     End Sub
    42.  
    43.     Private Sub SaveFavouriteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFavouriteButton.Click
    44.         If FavouriteDisplayNameTextBox.Text = String.Empty Then
    45.             Throw New ArgumentException("You must provide a Display Name for the Favourite")
    46.         End If
    47.  
    48.         If FavouriteUrlTextBox.Text = String.Empty Then
    49.             Throw New ArgumentException("You must provide a Url for the Favourite")
    50.         End If
    51.  
    52.         Dim favouriteUri As Uri
    53.  
    54.         If Not Uri.TryCreate(FavouriteUrlTextBox.Text, UriKind.RelativeOrAbsolute, favouriteUri) Then
    55.             Throw New ArgumentException("Please provide a valid Url")
    56.         End If
    57.  
    58.         Dim newFavourite As New Favourite()
    59.         newFavourite.DisplayText = FavouriteDisplayNameTextBox.Text
    60.         newFavourite.Url = favouriteUri
    61.  
    62.         Dim favouriteFileName As String
    63.         favouriteFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Favorites), newFavourite.DisplayText + ".url")
    64.  
    65.         WritePrivateProfileString("InternetShortcut", "URL", newFavourite.Url.ToString(), favouriteFileName)
    66.  
    67.         ListBox1.Items.Add(newFavourite)
    68.     End Sub

    Here, I do some sense checking to make sure that there has been something added to each of the TextBoxes, the favourite is written to the file system in the the Favourites folder, using the call to WritePrivateProfileString, and then I add it to the ListBox Items as well.

    Hope that helps!!

    Gary

  14. #14
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    Have you had a chance to try the above?

    Did it work?

    Gary

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    31

    Cool Re: Web Browser with favorites

    Yes,

    You know to gather all the URL files in the favourites folder and show them in a listbox.

    Would it be possible to do the same thing in a combo box but for folders instead?

    Using this code I think I'll be able to solve the problem concerning the folders.

    Thanks,

    m1la

  16. #16
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    Sure, adding it to a ComboBox would be very similar to adding it to a list view.

    If you provide details about exactly what you are trying to achieve, then I am sure I can help you out.

    Gary

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    31

    Cool Re: Web Browser with favorites

    Hi,

    I'd like all the folders in the favourites folder to be added to items list in the combo box so when you click on one it brings up that folder. So if in your favourites you had a folder called VB, when you selected it in the combo box then the links in the folder would appear in the list box (instead of the links already there).

    But if there was another folder in the "VB" folder would it be possible for that to be shown in the combo box?

    Thanks,

    m1la

  18. #18
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    Just so we are clear, are you now specifically talking about mimicing the functionality within Internet Explorer, where you can select where you want to add a favourite to?

    Gary

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    31

    Re: Web Browser with favorites

    This is for viewing the favourites not adding them. So that you can open faovurites within folders in the favourites folder.

  20. #20
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    I was actually playing with that yesterday.

    In my opinion, a ComboBox is the wrong control for this, you would be better off with a TreeView Control.

    For that, you will have to modify some of what we have already created. Firstly, the Favourite Class changes slightly to become:

    vb Code:
    1. Public Class FavouriteTreeNode
    2.     Inherits TreeNode
    3.  
    4.     Private _url As Uri
    5.     Public Property Url() As Uri
    6.         Get
    7.             Return _url
    8.         End Get
    9.         Set(ByVal value As Uri)
    10.             _url = value
    11.         End Set
    12.     End Property
    13.  
    14.     Public Sub New()
    15.  
    16.     End Sub
    17.  
    18.     Public Sub New(ByVal displayText As String, ByVal url As Uri)
    19.         Me.Text = displayText
    20.         Me.Url = url
    21.     End Sub
    22.  
    23. End Class

    Notice here, I am inheriting from the TreeNode class, adding an additional Url property.

    Next up, add a TreeView to your form called FavouritesTreeView, and add the following Subs:

    vb Code:
    1. Private Sub GetFavouritesForDirectory(ByVal di As DirectoryInfo, ByVal dirNode As TreeNode)
    2.         For Each fileinfo As FileInfo In di.GetFiles()
    3.             result = GetPrivateProfileString("InternetShortcut", "URL", "", sb, sb.Capacity, fileinfo.FullName)
    4.             If result > 0 Then
    5.                 Dim myFav As New FavouriteTreeNode()
    6.                 myFav.Text = Path.GetFileNameWithoutExtension(fileinfo.FullName)
    7.                 myFav.Url = New Uri(sb.ToString())
    8.                 If dirNode Is Nothing Then
    9.                     FavouritesTreeView.Nodes.Add(myFav)
    10.                 Else
    11.                     dirNode.Nodes.Add(myFav)
    12.                 End If
    13.             End If
    14.         Next
    15.     End Sub
    16.  
    17.     Private Sub GetFavourites()
    18.         For Each dirName As String In Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Favorites))
    19.             Dim dirInfo As New DirectoryInfo(dirName)
    20.             Dim NewNode As New TreeNode()
    21.             NewNode.Text = dirInfo.Name
    22.             NewNode.Tag = dirInfo.FullName
    23.             FavouritesTreeView.Nodes.Add(NewNode)
    24.             NewNode.Nodes.Add("*")
    25.         Next
    26.  
    27.         GetFavouritesForDirectory(New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites)), Nothing)
    28.     End Sub

    Call GetFavourites from your Form's Load event.

    Then, add the following code to your TreeView's BeforeExpand and NodeMouseDoubleClick events:

    vb Code:
    1. Private Sub FavouritesTreeView_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles FavouritesTreeView.BeforeExpand
    2.         If e.Node.Nodes(0).Text = "*" Then
    3.             e.Node.Nodes.Clear()
    4.             Me.GetFavouritesForDirectory(New DirectoryInfo(e.Node.Tag.ToString()), e.Node)
    5.         End If
    6.     End Sub
    7.  
    8.     Private Sub FavouritesTreeView_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles FavouritesTreeView.NodeMouseDoubleClick
    9.         Dim myFav As FavouriteTreeNode = DirectCast(e.Node, FavouriteTreeNode)
    10.         Me.currentBrowser.Navigate(myFav.Url)
    11.     End Sub

    The end result is the following:

    Name:  Favourites TreeView.png
Views: 362
Size:  3.7 KB

    How does that look?

    Gary

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    31

    Re: Web Browser with favorites

    This part of the test come up with a warning (the part inbetween the smiles)
    It says that sb has been used before it has been assigned a value.

    Private Sub GetFavouritesForDirectory(ByVal di As IO.DirectoryInfo, ByVal dirNode As TreeNode)
    Dim sb As System.Text.StringBuilder
    Dim result As Integer
    For Each fileinfo As IO.FileInfo In di.GetFiles()
    result = GetPrivateProfileString("InternetShortcut", "URL", "", sb, sb.Capacity, fileinfo.FullName)
    If result > 0 Then
    Dim myFav As New FavouritesTreeNodes()
    myFav.Text = IO.Path.GetFileNameWithoutExtension(fileinfo.FullName)
    myFav.Url = New Uri(sb.ToString())
    If dirNode Is Nothing Then
    FavouritesTreeView.Nodes.Add(myFav)
    Else
    dirNode.Nodes.Add(myFav)
    End If
    End If
    Next
    End Sub

    Thanks,

    m1la

  22. #22
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    Ah, my bad, I refactored that section from the original to make those members available at the class level.

    Put the following near the top of your code file, within the class definition:

    vb Code:
    1. Private sb As New StringBuilder(500)
    2.     Private result As Integer

    And put the GetFavouritesForDirectory back to the way it was:

    vb Code:
    1. Private Sub GetFavouritesForDirectory(ByVal di As DirectoryInfo, ByVal dirNode As TreeNode)
    2.         For Each fileinfo As FileInfo In di.GetFiles()
    3.             result = GetPrivateProfileString("InternetShortcut", "URL", "", sb, sb.Capacity, fileinfo.FullName)
    4.             If result > 0 Then
    5.                 Dim myFav As New FavouriteTreeNode()
    6.                 myFav.Text = Path.GetFileNameWithoutExtension(fileinfo.FullName)
    7.                 myFav.Url = New Uri(sb.ToString())
    8.                 If dirNode Is Nothing Then
    9.                     FavouritesTreeView.Nodes.Add(myFav)
    10.                 Else
    11.                     dirNode.Nodes.Add(myFav)
    12.                 End If
    13.             End If
    14.         Next
    15.     End Sub

    Gary

  23. #23

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    31

    Cool Re: Web Browser with favorites

    Hi,

    On post 18 you mentioned about saving favourites to specific folders.

    Would this be possible?

    Also would you be able to make new folders to put the links in?

    Lastly using context menus is there a way to delete the favourites?

    Thanks,

    M1la

    PS. If these would take a long time to create the code for, it doesn't matter

    Thanks Again for all your help!

  24. #24
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    I am actually working on creating a complete sample that includes all this functionality, I will try to include these most recent requests. Shouldn't be too difficult, I will try and get back to you tomorrow.

    Gary

  25. #25

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    31

    Smile Re: Web Browser with favorites

    Hi,

    Is it possible?

    Thanks,
    m1la

  26. #26
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    It is certainly possible, I just haven't had a chance to complete the sample. hopefully, I will hopefully get round to it this, this weekend.

    Gary

  27. #27

    Thread Starter
    Junior Member
    Join Date
    Jun 2009
    Posts
    31

    Cool Re: Web Browser with favorites

    How you getting on?

    m1la

  28. #28
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    It's coming along, almost at a point I can post it, but still needs some more work.

    Gary

  29. #29
    ASP.NET Moderator gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Web Browser with favorites

    Hey,

    Have a look at this post here:

    http://www.vbforums.com/showthread.p...42#post3769342

    Let me know if this isn't what you are after, and I will edit it to include what you are after.

    Gary

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width