Question Visual Basic Browser Isssue

jakeevans

Member
Joined
Mar 8, 2012
Messages
5
Programming Experience
Beginner
Hello Everyone,

I hope this is the right place i'm posting
this, basically I need some help with Visual Basic 2008.

We've been set a task to build a
really basic web browser with one advanced function. I've done the web browser
with the basic functionality, but this advanced feature really has got me
stumped.

The advanced feature is a "most
visited site" feature. I need to create some kind of listbox, that keeps track
of all the websites visited (when the go button is pressed) and arranges them in
the most popular order (i.e. the one that has been clicked the most)

Furthermore, it states that substrings
should be used so that for example if you go to http://vbdotnetforums.com and go to http://vbdotnetforums.com/test that counts as 1 hit.

I've been breaking my head out, because I'm sure its something to do with arrays,
but he did say you will need to use multiple procedures/strings/functions....

Hope someone out there can help me :smile:

Thanks,
Jake
 
should be used so that for example if you go to http://vbdotnetforums.com and go to http://vbdotnetforums.com/test that counts as 1 hit.
What you're talking about here is called the host part of the uri, and is available from the Host property of urls the browser provides as Uri instances.

My suggestion is this, use a Dictionary with host as key and number of hits as value. With a bit of Linq you can order them and select the top ten, then show that list in Listbox.

Effectively being two lines of code I'm sure this is not what the teacher prescibed, but there is multiple procedures/strings/functions....
    Private hosts As New Dictionary(Of String, Integer)

    Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If Me.WebBrowser1.ReadyState = WebBrowserReadyState.Complete AndAlso e.Url = Me.WebBrowser1.Url Then
            hosts(e.Url.Host) = If(hosts.ContainsKey(e.Url.Host), hosts(e.Url.Host) + 1, 1)
            Me.ListBox1.DataSource = (From host In hosts Order By host.Value Descending Take 10 Select host.Key).ToArray
        End If
    End Sub

The "e.Url = Me.WebBrowser1.Url" part is to avoid counting possible frames pages multiple times.

For bonus points you can for example save the hosts history to xml like this (for example from FormClosing event handler):
Dim x = <hosts><%= From host In hosts Select <host url=<%= host.Key %> count=<%= host.Value %>/> %></hosts>
x.Save("hosts.xml")

and load from same file (for example from Load event handler):
        If IO.File.Exists("hosts.xml") Then
            hosts = (From host In XDocument.Load("hosts.xml").<hosts>.<host>).ToDictionary(Function(x) x.@url, Function(x) CInt(x.@count))
        End If
 
Hi John,

Your an absolute star! Thank you so so much.
I'm going to try this when I get home tonight, I really hope this works.
I've been breaking my head for over a week on figuring how to do this.
Thanks once again! You've just made me really happy - just to know that their is a solution to this issue.

Thanks once again :D,
Jake
 
John! It works! Thank you so so much!
Just had two more questions to ask.

1) I am using the hosts.xml file saving/loading code you've given, but I need a code so that when a user wants to clear their most visited history and start again.
Is this possible?

Thanks,Jake
 
Hi John,

Many thanks for the code.
I've implemented it, and it deletes the file ok, problem is that I want it to reflect the changes immidiately so when they press the button it clears the listbox and starts again.
However, when I delete it, its still shows the listbox with the data inside it. I've tried listbox1.Items.ClearAll() but its just crashing out saying something about the data source.

Also when I close the form, because I added the save datasource code, its not deleting it, instead its ccreating it on close from the data whats already there.

Is there a solution to this?

Thanks,
Jake
 
something about the data source
Me.ListBox1.DataSource = Nothing

data whats already there
hosts.Clear()
 
Back
Top