Results 1 to 19 of 19

Thread: Whats the best way to avoid adding duplicates to a listbox?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2000
    Posts
    1,195

    Whats the best way to avoid adding duplicates to a listbox?

    in vb.net?

  2. #2
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    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.
    www.vb-tech.com
    .Net Freelance Development
    http://weblog.vb-tech.com/nick
    My blog

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2000
    Posts
    1,195
    its not like that...

  4. #4
    Junior Member
    Join Date
    Apr 2003
    Posts
    18
    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
    www.WinMgmt.com
    [email protected]

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Very easy way ...

    VB Code:
    1. If listbox.Items.Count = 0 Then
    2. listbox.Items.AddRange(fields)
    3. Else
    4. Exit Sub
    5. End If

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2000
    Posts
    1,195
    is there another way? is it possible to add all the items first, and then check for duplicates and remove them after?

  7. #7
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 ?

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2000
    Posts
    1,195
    what is fields?

  9. #9
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by qpabani
    what is fields?
    Uh..that's array of returned rows . How are you populating the listbox ?

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2000
    Posts
    1,195
    VB Code:
    1. List1.Items.Add("Hello")
    2.         List1.Items.Add("Hello")
    3.         List1.Items.Add("Hello")
    4.  
    5.         If ListBox1.Items.Count = 0 Then
    6.             ListBox1.Items.AddRange(List1.Items)
    7.         Else
    8.             Exit Sub
    9.         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..

  11. #11
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    I know it won't work the way you have done it , rather you can do it this way :

    VB Code:
    1. Dim Arry() as String ={"item1","item2","item"}
    2.  
    3.         If ListBox1.Items.Count = 0 Then
    4.             ListBox1.Items.AddRange(Arry)
    5.         Else
    6.         Exit Sub
    7.         End If

  12. #12
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2000
    Posts
    1,195
    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?

  14. #14
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'assuming these are strings and one is assigned to the item variable
    2. If Not Listbox1.items.Contains(item) then
    3.     Listbox1.items.Add(item)
    4. End If

  15. #15
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    edit....nm im just gonna setup a loop threw to look for dups

    AcE

  16. #16
    Lively Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    95
    I agree with Edneeis, check the list before adding items rather than add them then delete duplicates.

  17. #17
    Addicted Member theonetrueace's Avatar
    Join Date
    Jan 2002
    Location
    South Alabama
    Posts
    196
    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

  18. #18
    Lively Member
    Join Date
    May 2002
    Posts
    94
    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

  19. #19
    Lively Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    95
    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
  •  



Click Here to Expand Forum to Full Width