-
Mar 7th, 2010, 05:56 AM
#1
Thread Starter
Junior Member
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
-
Mar 7th, 2010, 06:06 AM
#2
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.
-
Mar 7th, 2010, 12:38 PM
#3
Thread Starter
Junior Member
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
-
Mar 7th, 2010, 01:52 PM
#4
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:
Public Class Favourite Private _displayText As String Public Property DisplayText() As String Get Return _displayText End Get Set(ByVal value As String) _displayText = value End Set End Property Private _url As Uri Public Property Url() As Uri Get Return _url End Get Set(ByVal value As Uri) _url = value End Set End Property Public Sub New() End Sub Public Sub New(ByVal displayText As String, ByVal url As Uri) Me.DisplayText = displayText Me.Url = url End Sub Public Overrides Function ToString() As String Return Me.DisplayText End Function 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:
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
Or, if you prefer, you could do it like this:
vb Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim myFavourite As New Favourite() myFavourite.DisplayText = "Google" myFavourite.Url = New Uri("http://www.google.com") Me.ListBox1.Items.Add(myFavourite) 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:
Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick Dim selectedFavourite As Favourite = DirectCast(ListBox1.SelectedItem, Favourite) Me.WebBrowser1.Navigate(selectedFavourite.Url) End Sub
Hope that helps!!
Gary
Last edited by gep13; Mar 7th, 2010 at 01:56 PM.
-
Mar 9th, 2010, 03:05 AM
#5
Re: Web Browser with favorites
Hey,
Out of interest, did this work for you? Or are you still having issues?
Gary
-
Mar 9th, 2010, 04:20 PM
#6
Thread Starter
Junior Member
Re: Web Browser with favorites
I'm going to try it at the weekend as I'm busy now.
Thanks,
m1la
-
Mar 10th, 2010, 02:36 AM
#7
Re: Web Browser with favorites
Hey,
Ah ok, no probs.
Let me know if you have any questions about it.
Gary
-
Mar 14th, 2010, 05:01 AM
#8
Thread Starter
Junior Member
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
-
Mar 14th, 2010, 05:54 AM
#9
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
-
Mar 15th, 2010, 04:08 AM
#10
Re: Web Browser with favorites
Hey,
Did this work for you?
Gary
-
Mar 18th, 2010, 11:34 AM
#11
Thread Starter
Junior Member
Re: Web Browser with favorites
Hi,
It works!!!
Thanks,
Do you know how I could add favourites?
Thanks again,
m1la
-
Mar 18th, 2010, 04:23 PM
#12
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
-
Mar 19th, 2010, 03:24 AM
#13
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:
<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 <DllImport("kernel32.dll", SetLastError:=True)> _ Private Shared Function WritePrivateProfileString(ByVal lpAppName As String, _ ByVal lpKeyName As String, _ ByVal lpString As String, _ 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 Private Sub ListBox1_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick Dim selectedFavourite As Favourite = DirectCast(ListBox1.SelectedItem, Favourite) Me.WebBrowser1.Navigate(selectedFavourite.Url) End Sub Private Sub SaveFavouriteButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveFavouriteButton.Click If FavouriteDisplayNameTextBox.Text = String.Empty Then Throw New ArgumentException("You must provide a Display Name for the Favourite") End If If FavouriteUrlTextBox.Text = String.Empty Then Throw New ArgumentException("You must provide a Url for the Favourite") End If Dim favouriteUri As Uri If Not Uri.TryCreate(FavouriteUrlTextBox.Text, UriKind.RelativeOrAbsolute, favouriteUri) Then Throw New ArgumentException("Please provide a valid Url") End If Dim newFavourite As New Favourite() newFavourite.DisplayText = FavouriteDisplayNameTextBox.Text newFavourite.Url = favouriteUri Dim favouriteFileName As String favouriteFileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Favorites), newFavourite.DisplayText + ".url") WritePrivateProfileString("InternetShortcut", "URL", newFavourite.Url.ToString(), favouriteFileName) ListBox1.Items.Add(newFavourite) 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
-
Mar 20th, 2010, 04:53 AM
#14
Re: Web Browser with favorites
Hey,
Have you had a chance to try the above?
Did it work?
Gary
-
Mar 21st, 2010, 09:32 AM
#15
Thread Starter
Junior Member
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
-
Mar 21st, 2010, 10:20 AM
#16
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
-
Mar 21st, 2010, 10:46 AM
#17
Thread Starter
Junior Member
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
-
Mar 21st, 2010, 10:54 AM
#18
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
-
Mar 21st, 2010, 11:00 AM
#19
Thread Starter
Junior Member
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.
-
Mar 21st, 2010, 11:10 AM
#20
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:
Public Class FavouriteTreeNode Inherits TreeNode Private _url As Uri Public Property Url() As Uri Get Return _url End Get Set(ByVal value As Uri) _url = value End Set End Property Public Sub New() End Sub Public Sub New(ByVal displayText As String, ByVal url As Uri) Me.Text = displayText Me.Url = url End Sub 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:
Private Sub GetFavouritesForDirectory(ByVal di As DirectoryInfo, ByVal dirNode As TreeNode) For Each fileinfo As FileInfo In di.GetFiles() result = GetPrivateProfileString("InternetShortcut", "URL", "", sb, sb.Capacity, fileinfo.FullName) If result > 0 Then Dim myFav As New FavouriteTreeNode() myFav.Text = 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 Private Sub GetFavourites() For Each dirName As String In Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Favorites)) Dim dirInfo As New DirectoryInfo(dirName) Dim NewNode As New TreeNode() NewNode.Text = dirInfo.Name NewNode.Tag = dirInfo.FullName FavouritesTreeView.Nodes.Add(NewNode) NewNode.Nodes.Add("*") Next GetFavouritesForDirectory(New DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Favorites)), Nothing) 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:
Private Sub FavouritesTreeView_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles FavouritesTreeView.BeforeExpand If e.Node.Nodes(0).Text = "*" Then e.Node.Nodes.Clear() Me.GetFavouritesForDirectory(New DirectoryInfo(e.Node.Tag.ToString()), e.Node) End If End Sub Private Sub FavouritesTreeView_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles FavouritesTreeView.NodeMouseDoubleClick Dim myFav As FavouriteTreeNode = DirectCast(e.Node, FavouriteTreeNode) Me.currentBrowser.Navigate(myFav.Url) End Sub
The end result is the following:
How does that look?
Gary
-
Mar 21st, 2010, 12:21 PM
#21
Thread Starter
Junior Member
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
-
Mar 21st, 2010, 12:29 PM
#22
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:
Private sb As New StringBuilder(500) Private result As Integer
And put the GetFavouritesForDirectory back to the way it was:
vb Code:
Private Sub GetFavouritesForDirectory(ByVal di As DirectoryInfo, ByVal dirNode As TreeNode) For Each fileinfo As FileInfo In di.GetFiles() result = GetPrivateProfileString("InternetShortcut", "URL", "", sb, sb.Capacity, fileinfo.FullName) If result > 0 Then Dim myFav As New FavouriteTreeNode() myFav.Text = 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
Gary
-
Mar 22nd, 2010, 03:04 PM
#23
Thread Starter
Junior Member
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!
-
Mar 22nd, 2010, 03:19 PM
#24
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
-
Mar 24th, 2010, 02:00 PM
#25
Thread Starter
Junior Member
Re: Web Browser with favorites
Hi,
Is it possible?
Thanks,
m1la
-
Mar 24th, 2010, 05:14 PM
#26
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
-
Apr 1st, 2010, 06:41 AM
#27
Thread Starter
Junior Member
Re: Web Browser with favorites
-
Apr 1st, 2010, 07:04 AM
#28
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
-
Apr 3rd, 2010, 09:07 AM
#29
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|