Problem: Filtering similar items in a listbox

r00tb0x

New member
Joined
Nov 30, 2010
Messages
2
Programming Experience
Beginner
I have a listbox already filled with links from a search engine. I am trying to filter out some of the links from the same domain:

Error
http://abc.net.au/news/stories/test/index.html
Test cricket - Wikipedia, the free encyclopedia <---- remove this link because same as first entry

I've tried many things with no success. Here is my existing code for FilterButton_Click:

VB.NET:
    Private Sub FilterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FilterButton.Click

        'Search and remove same site links'

        Dim arrayclean As ArrayList = New ArrayList()
        Dim thing As String
        For i As Integer = ResultsBox.Items.Count - 1 To 0 Step -1
            Dim check As Match = System.Text.RegularExpressions.Regex.Match(ResultsBox.Items(i), "http://.*?/")
            If ResultsBox.Items(i).Contains(check.ToString) Then

            Else
                arrayclean.Add(ResultsBox.Items.Item(i))
            End If
        Next
        ResultsBox.Items.Clear()
        For Each thing In arrayclean
            ResultsBox.Items.Add(thing)
        Next
        ResultsCnt.Text = ResultsBox.Items.Count
    End Sub

It seems so simple in my head, it just isn't working for me.
 
VB.NET:
Dim hosts As New Dictionary(Of String, String)
For Each url As String In Me.ResultsBox.Items
    hosts(New Uri(url).Host) = url
Next
Me.ResultsBox.DataSource = New List(Of String)(hosts.Values)
 
That worked beautifully. Thanks a million. I had never heard of the dictionary class or the uri.host property.
 
Another option would be to let LINQ enumerate your Items collection and select only the Host part of your Uri.

VB.NET:
        Me.ResultsBox.DataSource = (From url In Me.ResultsBox.Items
                                  Select New Uri(url).Host).Distinct.ToList
 
Last edited:
Back
Top