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 :)
LVL 1
BraidenGAsked:
Who is Participating?
 
Wayne Taylor (webtubbs)Connect With a Mentor Commented:
Set the DrawMode of the Listbox to OwnerDrawVariable, then use code like below to populate it.

Wayne
    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)
            If arr.Length = 3 Then
                Me.ListBox1.Items.Add("Header" & vbCrLf & arr(0) & vbCrLf & arr(1) & vbCrLf & arr(2))
            End If
        Next
    End Sub 
    Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
        e.Graphics.DrawString(ListBox1.Items(e.Index), ListBox1.Font, Brushes.Black, e.Bounds)
    End Sub 
    Private Sub ListBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ListBox1.MeasureItem
        e.ItemHeight = e.ItemHeight * 4.05
    End Sub

Open in new window

0
 
Wayne Taylor (webtubbs)Commented:
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

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

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

Thanks :)
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Wayne Taylor (webtubbs)Commented:
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

0
 
BraidenGAuthor Commented:
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 :)
0
 
BraidenGAuthor Commented:
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

0
 
BraidenGAuthor Commented:
Thanks alot, helped greatly, also added an extra comment/question about arrays & counting it. Thanks :)
0
 
Wayne Taylor (webtubbs)Commented:
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
0
 
BraidenGAuthor Commented:
Using that code, it now seems to loop through the string twice. Any ideas why or how to stop this?

Thanks :)
0
 
Wayne Taylor (webtubbs)Commented:
It only loops through once.
0
 
BraidenGAuthor Commented:
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

0
 
Wayne Taylor (webtubbs)Commented:
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)?
0
 
BraidenGAuthor Commented:
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! :)
0
 
Wayne Taylor (webtubbs)Commented:
Glad I could help :)

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

Wayne
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.