Results 1 to 6 of 6

Thread: Removing Duplicates in Lisbox

Hybrid View

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    14

    Removing Duplicates in Lisbox

    how do i remove duplicates in a listbox??? please give code ---

    THANK YOU THANK YOU THANK YOU!!!!!

  2. #2
    Fanatic Member dongaman's Avatar
    Join Date
    Aug 2001
    Location
    xi'an
    Posts
    616
    It is a base arithmetic.
    How do you programming?????????????
    I am just aman.

  3. #3
    Lively Member
    Join Date
    Mar 2000
    Location
    LewZer-LanD
    Posts
    120
    on a form create a listbox named list1 and put the following code in a commandbox

    Code:
    on error resume next
    for n = 0 to list1.listcount - 1  'loop the listbox
      cur = list1.list(n)  'sets the current list item
      for m = 0 to list1.listcount - 1  'now loop for the victims
        if m <> n and list1.list(m) = cur then  'we found a match!
          list1.removeitem m  'now remove it
          'add more code here to execute when finding a match
        end if
      next m
    next n
    ^^^ this code will look for matches by case
    example: 'abc' will match with 'abc' but not 'ABC'

    if you do not want it case sensitive.. modify the code above with this line:

    Code:
        if m <> n and ucase$(list1.list(m)) = ucase$(cur) then
    ^^^now it will look for matches by similarity
    example: 'abc' will match with 'abc' and 'ABC'
    Kid A

    18 Year Old Programmer
    Visual Basic 6 & .NET Enterprise, ASP, WinXP (Advanced Server) Administration, HTML, Graphic Arts, Winsock, Learning VC++ and now maybe C#.. heh
    [vbcode]
    'back in the day vb6 code
    Private Sub My_Life()
    If Hour(Now) > 3 And Hour(Now) < 13 Then
    Status = "Sleeping"
    Else
    Status = "Computing"
    End If
    End Sub
    [/vbcode]

  4. #4
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    2 things
    1) Any time u remove items from a list box you should always loop backwards. In the prev code from da404 u should change loops to ....
    VB Code:
    1. on error resume next
    2. [b]for n = list1.listcount - 1 to 0 Step - 1 [/b] 'loop the listbox
    3.   cur = list1.list(n)  'sets the current list item
    4.   [b]for m = list1.listcount - 1 to 0 Step -1 [/b] 'now loop for the victims
    5.     if m <> n and list1.list(m) = cur then  'we found a match!
    6.       list1.removeitem m  'now remove it
    7.       'add more code here to execute when finding a match
    8.     end if
    9.   next m
    10. next n

    2) Secondly, that code will be ok for small amounts of items but if u want to remove duplicates from a large list then it is much quicker to sort the list first and then loop thru it only once removing duplicates

    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    beachbum: Nice little routine. I have something that I did to do this, but your routine is much, much tighter.

  6. #6
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi Hack
    I didnt write that routine... only modified it to work. It was da404's.
    regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

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