Results 1 to 14 of 14

Thread: Check listbox for duplicate items and add suffix if they exist

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Check listbox for duplicate items and add suffix if they exist

    I'm loading some names and informations into listbox and some of them have the same name but different informations, I don't want to remove them I just want to add suffix, something like numbers. Listbox can have 10 or 100 same names so I just need to add number on each duplicate name, can anyone help?

  2. #2
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Check listbox for duplicate items and add suffix if they exist

    I'm guessing there are more elegant ways of doing this but you can do this..
    Code:
          Dim sArrayOfItems As New List(Of String)
            Dim sArrayOfAlreadyChecked As New List(Of String)
    
            'Grab the existing items
            For Each s As String In Me.ListBox1.Items
                sArrayOfItems.Add(s)
            Next
    
            'Clear the items out of the listbox
            Me.ListBox1.Items.Clear()
    
            'See which match and append the number of times.
            For Each s2 As String In sArrayOfItems
                If Not sArrayOfAlreadyChecked.Contains(s2) Then
                    sArrayOfAlreadyChecked.Add(s2)
                    Dim icount As Integer = 0
                    For Each t As String In sArrayOfItems
                        If t = s2 Then
                            icount += 1
                            Me.ListBox1.Items.Add(s2 & " " & icount.ToString)
                        End If
                    Next
                End If
            Next
    Downside is it resorts the items.
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Check listbox for duplicate items and add suffix if they exist

    this is what i'd recommend for your suffix:

    first distinct name(1)
    first distinct name(2)
    'etc
    second distinct name(1)
    second distinct name(2)
    'etc

    you can use code like this to count and change them:

    Code:
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        For x As Integer = 0 To ListBox1.Items.Count - 1
            ListBox1.Items(x) = System.Text.RegularExpressions.Regex.Replace(ListBox1.Items(x).ToString, "\(\d+\)", "")
        Next
        Dim listItems() As String = ListBox1.Items.Cast(Of String).Distinct.ToArray
        Dim d As New Dictionary(Of String, Integer)
        For x As Integer = 0 To listItems.GetUpperBound(0)
            d.Add(listItems(x), 0)
        Next
        For x As Integer = 0 To ListBox1.Items.Count - 1
            d(ListBox1.Items(x).ToString) += 1
            ListBox1.Items(x) = ListBox1.Items(x).ToString & "(" & d(ListBox1.Items(x).ToString) & ")"
        Next
    End Sub

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: Check listbox for duplicate items and add suffix if they exist

    Thanks guys, .paul. what am I doing wrong?


  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Check listbox for duplicate items and add suffix if they exist

    which version of vb are you using? targeting below 3.5 framework wouldn't work

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Check listbox for duplicate items and add suffix if they exist

    ok. what is the exact format and method you're using to add to your listbox? is it databound?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: Check listbox for duplicate items and add suffix if they exist

    Quote Originally Posted by .paul. View Post
    which version of vb are you using? targeting below 3.5 framework wouldn't work
    VB 2010 & .Net Framework 3.5
    Last edited by public; May 6th, 2014 at 05:53 PM.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Check listbox for duplicate items and add suffix if they exist

    did you miss post #6?

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: Check listbox for duplicate items and add suffix if they exist

    Quote Originally Posted by .paul. View Post
    ok. what is the exact format and method you're using to add to your listbox? is it databound?
    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Dim nm As String = RichTextBox1.Text
            Dim r As New Regex("(?<="">).+?(?=</a></h4>)")
            Dim matches As MatchCollection = r.Matches(nm)
            For Each name As Match In matches
                ListBox1.Items.Add(name.Value)
            Next
    
            Dim r2 As New Regex("(?<=" href=").+?(?=">)")
            Dim matches2 As MatchCollection = r2.Matches(nm)
            For Each number As Match In matches2
                ListBox2.Items.Add(number.Value)
            Next
    End Sub

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Check listbox for duplicate items and add suffix if they exist

    in that case my friend (you see), check your references:

    Name:  06-05-2014_23.54.49.jpg
Views: 139
Size:  23.0 KB

    you dig???

    the important ones are system.core and system.xml.linq

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Check listbox for duplicate items and add suffix if they exist

    if system.core is missing, it's a particularly tricky one to change. The easiest way is to create a new project in vb2010, change your framework to 3.5, then check the references, and add the old project's forms manually using Project-->Add Existing Item

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: Check listbox for duplicate items and add suffix if they exist

    Quote Originally Posted by .paul. View Post
    if system.core is missing, it's a particularly tricky one to change. The easiest way is to create a new project in vb2010, change your framework to 3.5, then check the references, and add the old project's forms manually using Project-->Add Existing Item
    You're the legend!

    One more thing, can I have just (2), (3) etc. without (1)?

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Check listbox for duplicate items and add suffix if they exist

    In the last line in the last loop where Names.Items(x) is changed, use a ternary statement (inline if statement):

    Names.Items(x) = Names.Items(x).ToString & If(d(Names.Items(x).ToString) > 1, Names.Items(x).ToString & "(" & d(Names.Items(x).ToString) & ")", "")

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: Check listbox for duplicate items and add suffix if they exist

    Quote Originally Posted by .paul. View Post
    In the last line in the last loop where Names.Items(x) is changed, use a ternary statement (inline if statement):

    Names.Items(x) = Names.Items(x).ToString & If(d(Names.Items(x).ToString) > 1, Names.Items(x).ToString & "(" & d(Names.Items(x).ToString) & ")", "")
    I can't thank you enough! Amazing! Thank you very much!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width