|
-
Apr 29th, 2003, 02:48 PM
#1
Thread Starter
Frenzied Member
Whats the best way to avoid adding duplicates to a listbox?
-
Apr 29th, 2003, 03:02 PM
#2
Fanatic Member
if you are getting the values from a database using a SQL statement you could do select distinct row and i think that will only bring out individual values.
-
Apr 29th, 2003, 03:15 PM
#3
Thread Starter
Frenzied Member
-
Apr 29th, 2003, 04:08 PM
#4
Junior Member
Add a Key Value by inherting the ListViewItem in your own class like below. Then iterate through the keys every time you add a listitem to see if it exsists. Don't worry This is very fast due to the IDictionaryEnumerator Interface.
Public Class MyListViewItem
Inherits System.Windows.Forms.ListViewItem
Implements IDictionaryEnumerator
Private ListItemEntry As DictionaryEntry
Private enumerator As IEnumerator
Public Sub New()
enumerator = MyBase.SubItems.GetEnumerator
End Sub
Public Property NodeKey() As String
Get
Return ListItemEntry.Key.ToString()
End Get
Set(ByVal Value As String)
ListItemEntry.Key = Value
End Set
End Property
Public Property NodeValue() As Object
Get
Return ListItemEntry.Value
End Get
Set(ByVal Value As Object)
ListItemEntry.Value = Value
End Set
End Property
Public Overridable Overloads ReadOnly Property Entry() As DictionaryEntry _
Implements IDictionaryEnumerator.Entry
Get
Return ListItemEntry
End Get
End Property
Public Overridable Overloads Function MoveNext() As Boolean _
Implements IDictionaryEnumerator.MoveNext
Dim Success As Boolean
Success = enumerator.MoveNext()
Return Success
End Function
Public Overridable Overloads ReadOnly Property Current() As Object _
Implements IEnumerator.Current
Get
Return enumerator.Current
End Get
End Property
Public Overridable Overloads ReadOnly Property Key() As Object _
Implements IDictionaryEnumerator.Key
Get
Return ListItemEntry.Key
End Get
End Property
Public Overridable Overloads ReadOnly Property Value() As Object _
Implements IDictionaryEnumerator.Value
Get
Return ListItemEntry.Value
End Get
End Property
Public Overridable Overloads Sub Reset() _
Implements IEnumerator.Reset
enumerator.Reset()
End Sub
End Class
-
Apr 29th, 2003, 04:29 PM
#5
Sleep mode
Very easy way ...
VB Code:
If listbox.Items.Count = 0 Then
listbox.Items.AddRange(fields)
Else
Exit Sub
End If
-
Apr 29th, 2003, 05:11 PM
#6
Thread Starter
Frenzied Member
is there another way? is it possible to add all the items first, and then check for duplicates and remove them after?
-
Apr 29th, 2003, 05:14 PM
#7
Sleep mode
Originally posted by qpabani
is there another way? is it possible to add all the items first, and then check for duplicates and remove them after?
Hmmm , have you tried that code I posted , I did the trick for me ?
-
Apr 29th, 2003, 05:33 PM
#8
Thread Starter
Frenzied Member
-
Apr 29th, 2003, 05:39 PM
#9
Sleep mode
Originally posted by qpabani
what is fields?
Uh..that's array of returned rows . How are you populating the listbox ?
-
Apr 29th, 2003, 05:55 PM
#10
Thread Starter
Frenzied Member
VB Code:
List1.Items.Add("Hello")
List1.Items.Add("Hello")
List1.Items.Add("Hello")
If ListBox1.Items.Count = 0 Then
ListBox1.Items.AddRange(List1.Items)
Else
Exit Sub
End If
doesnt work, it adds hello 3 times tothe second box..
in my program, im just adding items from data retrieved on a webpage, no arrays invovled..
-
Apr 29th, 2003, 05:59 PM
#11
Sleep mode
I know it won't work the way you have done it , rather you can do it this way :
VB Code:
Dim Arry() as String ={"item1","item2","item"}
If ListBox1.Items.Count = 0 Then
ListBox1.Items.AddRange(Arry)
Else
Exit Sub
End If
-
Apr 29th, 2003, 06:08 PM
#12
Try explaining more about where the data is coming from or how it is collected. The best way is not to put duplicate info into the listbox, but if its already there then you have to write a routine to find and delete dupes. I'd make use of the Contains method of the items collection before putting an item in.
-
Apr 29th, 2003, 06:16 PM
#13
Thread Starter
Frenzied Member
well.. the app retrieves data from numerous pages, parses the html and adds items to the listbox.. after its done this routine, i need to be able to remove the duplicates...
anyone know how?
-
Apr 29th, 2003, 08:34 PM
#14
Instead of just adding them all in, before one is added check if it is already there, this is by far the easiest most efficent way.
VB Code:
'assuming these are strings and one is assigned to the item variable
If Not Listbox1.items.Contains(item) then
Listbox1.items.Add(item)
End If
-
Apr 30th, 2003, 12:32 AM
#15
Addicted Member
edit....nm im just gonna setup a loop threw to look for dups
AcE
-
Apr 30th, 2003, 03:57 AM
#16
Lively Member
I agree with Edneeis, check the list before adding items rather than add them then delete duplicates.
-
Apr 30th, 2003, 11:52 AM
#17
Addicted Member
well heres a lil loop code i cam up with.....its very lame and there is something wrong...i cant put my fingure on it...tell me wut u think....this is ofcourse ran after the id is added....
Code:
Private Sub RunDupCheck()
If LV_IDList.Items.Count <> 0 Then
Dim i As Integer = LV_IDList.Items.Count - 1
Dim ii As Integer = LV_IDList.Items.Count - 1
Do Until i = -1
Do Until ii = -1
If i <> ii Then
If LV_IDList.Items(ii).Text = LV_IDList.Items(i).Text Then
LV_IDList.Items(ii).Remove()
End If
ii = ii - 1
Else
ii = ii - 1
End If
Loop
i = i - 1
Loop
End If
End Sub
AcE
-
May 1st, 2003, 03:37 AM
#18
Lively Member
Just like Edneeis stated, but this checks the text value.
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ListBox.Items.IndexOf("Hello World") = -1 Then
ListBox.Items.Add("Hello World")
Else
MsgBox("Item - Hello World already exists")
End If
End Sub
-
May 1st, 2003, 03:44 AM
#19
Lively Member
There are a couple of obvious problems, firstly when you would need to reset the 'ii' count before attempting to do the second loop again and when you remove an item from the list all the indexes are shifted by one, so 'i' can point to an item that no longer exists.
This seems to work, tho I've not compiled and run it!
Code:
Dim i as integer
Dim ii as integer
i = 0
Do While i < LV_IDList.Items.Count
ii = i + 1
Do While ii < LV_IDList.Items.Count
If LV_IDList.Items(i).Text = LV_IDList.Items(ii).Text Then
LV_IDList.Items(ii).Remove()
Else
ii += 1
End If
Loop
i += 1
Loop
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
|