Link to home
Start Free TrialLog in
Avatar of BraidenG
BraidenGFlag for New Zealand

asked on

How can I download a URL, and parse it into a Custom Listbox?

What I am looking to do, is download the text webpage/file:

http://hiscore.runescape.com/index_lite.ws?player=zezima

That will return a text file with 2 delimiters, a "," and a " ". What I firstly want to do is parse that file, first, by the <space> delimiter (into groups of sort), then parse the groups into individual values (using the "," delimiter).

From that, I would like to display the Values of each group in a Custom Listbox (I have seen it done using g.Draw etc.) that has 4 lines per 'item', the first is a header, then the 3 values of the group.

Any help would be appreciated :)
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Instead of a custom Listbox, use a ListView control with its View property set to Details. Add 3 columns, then use the below code to populate it.

Wayne
        Dim url As String = "http://hiscore.runescape.com/index_lite.ws?player=zezima"
        Dim wc As New Net.WebClient
        Dim txt As String = wc.DownloadString(url)
        For Each line As String In txt.Split(vbLf)
            Dim arr() As String = line.Split(","c)
            Dim lvi As ListViewItem = ListView1.Items.Add(arr(0))
            For i As Integer = 1 To UBound(arr)
                lvi.SubItems.Add(arr(i))
            Next
        Next

Open in new window

Avatar of BraidenG

ASKER

I was more looking for it to be in Rows, like my bad ASCII example below:

__________________________
| Header                                            |
| Value 1                                            |
| Value 2                                            |
| Value 3___________________|

Thanks :)
So you mean something like a Treeview?

Also, there are only 3 items per "group". Where does the header come from?

Wayne
        Dim url As String = "http://hiscore.runescape.com/index_lite.ws?player=zezima"
        Dim wc As New Net.WebClient
        Dim txt As String = wc.DownloadString(url)
        For Each line As String In txt.Split(vbLf)
            Dim arr() As String = line.Split(","c)
            Dim tn As TreeNode = TreeView1.Nodes.Add(arr(0))
            For i As Integer = 1 To UBound(arr)
                tn.Nodes.Add(arr(i))
            Next
        Next

Open in new window

The header is added by me, FE: from that url that is parsed, the first space delimited group would be "General" then the next space would be "Attack", those values I would add myself.

And, no not like a treeview, like a normal Listbox, instead the listbox items are 4 lines high, and each line can have a value associated with, almost as if you could call:

Listbox1.items.add(header,value1,value2,value3)

Thanks :)
ASKER CERTIFIED SOLUTION
Avatar of Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs)
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Brilliant!

Im also wondering, how would I add OnClick events, because ive noticed one I use the g.draw my clicks do nothing. Not that it really matters, just incase I want to do something more with it & to define the header of each 'group' should I just use an array of strings (and how would i declare it properly?), then count them using this, would it work?

'Define Header Array Here.
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim url As String = "http://hiscore.runescape.com/index_lite.ws?player=zezima"
        Dim wc As New Net.WebClient
        Dim txt As String = wc.DownloadString(url)
        For Each line As String In txt.Split(vbLf)
            Dim arr() As String = line.Split(","c)
            Dim i As Integer = -1
            If arr.Length = 3 Then
                i = i + 1
                Me.ListBox1.Items.Add(Header(i) & vbCrLf & arr(0) & vbCrLf & arr(1) & vbCrLf & arr(2))
            End If
        Next
    End Sub 

Open in new window

Thanks alot, helped greatly, also added an extra comment/question about arrays & counting it. Thanks :)
I forgot to include a couple of lines in the DrawItem event that enables selecting of items....

    Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        e.DrawBackground()
        e.DrawFocusRectangle()
        e.Graphics.DrawString(ListBox1.Items(e.Index), ListBox1.Font, Brushes.Black, e.Bounds)
    End Sub

Regarding your headers, if the number of lines remains the same, you can create a fixed array of header strings, and you'd modify the code like this....

        Dim url As String = "http://hiscore.runescape.com/index_lite.ws?player=zezima"
        Dim wc As New Net.WebClient
        Dim txt As String = wc.DownloadString(url)
        Dim Headers() As String = {"Header1", "Header2", "Header3", "Header4", "Header5", "Header6"}
        Dim Lines() As String = txt.Split(vbLf)
        For i As Integer = 0 To UBound(Headers)
            Dim arr() As String = Lines(i).Split(","c)
            If arr.Length = 3 Then
                Me.ListBox1.Items.Add(Headers(i) & vbCrLf & String.Join(vbCrLf, arr))
            End If
        Next

Wayne
Using that code, it now seems to loop through the string twice. Any ideas why or how to stop this?

Thanks :)
It only loops through once.
Hmm, well, when I use 25 headers ( there are 25 groups in that string ) I end up with the whole string looped twice. I will try using a different counting method, like the i = i + 1, and reply if it works or not. Below is the code that causes it to loop twice.

Thanks :)



        Dim url As String = "http://hiscore.runescape.com/index_lite.ws?player=zezima"
        Dim wc As New Net.WebClient
        Dim txt As String = wc.DownloadString(url)
        Dim Headers() As String = {"Header1", "Header2", "Header3", "Header4", "Header5", "Header6", "Header7", "Header8", "Header9", "Header10", "Header11", "Header12", "Header13", "Header14", "Header15", "Header16", "Header17", "Header18", "Header19", "Header20", "Header21", "Header22", "Header23", "Header24", "Header25"}
        ' 
        Dim Lines() As String = txt.Split(vbLf)
        For i As Integer = 0 To UBound(Headers)
            Dim arr() As String = Lines(i).Split(","c)
            If arr.Length = 3 Then
                Me.ListBox1.Items.Add(Headers(i) & vbCrLf & String.Join(vbCrLf, arr))
            End If
        Next

Open in new window

I only get 25 items added to the ListBox when using your code.

Is it possible you're running that code twice somehow (2 different locations)?
Yes, that was the problem, I am currently not on my proper Dev Machine, so I'm using SharpDevelop, which isnt anywhere near as good as MSVS.

Thank you. Your help has been extraordinarily good! :)
Glad I could help :)

BTW - if you're after a free IDE, try Visual Studio Express -> http://www.microsoft.com/express/

Wayne