-
May 6th, 2014, 03:23 PM
#1
Thread Starter
Lively Member
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?
-
May 6th, 2014, 03:50 PM
#2
Addicted Member
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
-
May 6th, 2014, 04:03 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 6th, 2014, 04:51 PM
#4
Thread Starter
Lively Member
Re: Check listbox for duplicate items and add suffix if they exist
Thanks guys, .paul. what am I doing wrong?
-
May 6th, 2014, 04:53 PM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 6th, 2014, 05:00 PM
#6
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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 6th, 2014, 05:16 PM
#7
Thread Starter
Lively Member
Re: Check listbox for duplicate items and add suffix if they exist
Originally Posted by .paul.
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.
-
May 6th, 2014, 05:44 PM
#8
Re: Check listbox for duplicate items and add suffix if they exist
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 6th, 2014, 05:53 PM
#9
Thread Starter
Lively Member
Re: Check listbox for duplicate items and add suffix if they exist
Originally Posted by .paul.
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
-
May 6th, 2014, 05:58 PM
#10
Re: Check listbox for duplicate items and add suffix if they exist
in that case my friend (you see), check your references:
you dig???
the important ones are system.core and system.xml.linq
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 6th, 2014, 06:03 PM
#11
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 6th, 2014, 06:43 PM
#12
Thread Starter
Lively Member
Re: Check listbox for duplicate items and add suffix if they exist
Originally Posted by .paul.
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)?
-
May 6th, 2014, 06:54 PM
#13
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) & ")", "")
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 6th, 2014, 07:04 PM
#14
Thread Starter
Lively Member
Re: Check listbox for duplicate items and add suffix if they exist
Originally Posted by .paul.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|