Results 1 to 9 of 9

Thread: How to remove duplicated values from listbox

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Unhappy How to remove duplicated values from listbox

    Hi guys. I got a listbox and after sorting it i found out that it has duplicated values. could any one tell me how to remove duplicated values from it. Furthermor, how to sort values in textbox and remove duplication from it.Thanks

  2. #2
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    266

    Re: How to remove duplicated values from listbox

    If ur list box is getting populated from database then use DISTINCT clause in query to avoid duplicate records.

  3. #3
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: How to remove duplicated values from listbox

    Quote Originally Posted by tony007
    Hi guys. I got a listbox and after sorting it i found out that it has duplicated values. could any one tell me how to remove duplicated values from it. Furthermor, how to sort values in textbox and remove duplication from it.Thanks
    To avoid duplicates when the listbox is being populated, compare every new item with all the items already present in the list.
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  4. #4
    PowerPoster lintz's Avatar
    Join Date
    Mar 2003
    Location
    The 19th Hole
    Posts
    2,697

    Re: How to remove duplicated values from listbox

    This will work....HTH

    VB Code:
    1. Option Explicit
    2. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    3. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    4. Private Const LB_FINDSTRING = &H18F
    5.  
    6.  
    7. Private Sub Form_Load()
    8. Dim ItemToAdd(3) As String
    9. Dim i As Integer
    10.  
    11. ItemToAdd(0) = "Lintz"
    12. ItemToAdd(1) = "was"
    13. ItemToAdd(2) = "here"
    14. ItemToAdd(3) = "Lintz"
    15.  
    16.     For i = 0 To 3
    17.    
    18.         If SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal ItemToAdd(i)) = -1 Then
    19.         List1.AddItem ItemToAdd(i)
    20.         End If
    21.    
    22.     Next
    23.  
    24.    
    25. End Sub

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to remove duplicated values from listbox

    Quote Originally Posted by noshaba
    If ur list box is getting populated from database then use DISTINCT clause in query to avoid duplicate records.
    Man my data does not come from db !!

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to remove duplicated values from listbox

    Quote Originally Posted by lintz
    This will work....HTH

    VB Code:
    1. Option Explicit
    2. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    3. (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    4. Private Const LB_FINDSTRING = &H18F
    5.  
    6.  
    7. Private Sub Form_Load()
    8. Dim ItemToAdd(3) As String
    9. Dim i As Integer
    10.  
    11. ItemToAdd(0) = "Lintz"
    12. ItemToAdd(1) = "was"
    13. ItemToAdd(2) = "here"
    14. ItemToAdd(3) = "Lintz"
    15.  
    16.     For i = 0 To 3
    17.    
    18.         If SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal ItemToAdd(i)) = -1 Then
    19.         List1.AddItem ItemToAdd(i)
    20.         End If
    21.    
    22.     Next
    23.  
    24.    
    25. End Sub
    Man i have 200 items in listbox and u want me to do like this 200 times ?!!!

    ItemToAdd(0) = "Lintz"
    ItemToAdd(1) = "was"
    ItemToAdd(2) = "here"
    ItemToAdd(3) = "Lintz"

    if i had that much time to write 200 times i was better deleting the dulicate a first place. lol. and i wish i know every time i how many items i have before hand.So i hope i get a better way then this.

  7. #7
    Fanatic Member vivek_master146's Avatar
    Join Date
    Apr 2006
    Location
    Delhi,India
    Posts
    787

    Thumbs up Re: How to remove duplicated values from listbox

    Here is the perfect code

    VB Code:
    1. Private Sub xListKillDupes(listbox As listbox)
    2. 'Kills dublicite items in a listbox
    3.         Dim Search1 As Long
    4.         Dim Search2 As Long
    5.         Dim KillDupe As Long
    6. KillDupe = 0
    7. For Search1& = 0 To listbox.ListCount - 1
    8. For Search2& = Search1& + 1 To listbox.ListCount - 1
    9. KillDupe = KillDupe + 1
    10. If listbox.List(Search1&) = listbox.List(Search2&) Then
    11. listbox.RemoveItem Search2&
    12. Search2& = Search2& - 1
    13. End If
    14. Next Search2&
    15. Next Search1&
    16. End Sub
    17.  
    18. Private Sub Command1_Click()
    19. Call xListKillDupes(listboxname)
    20. End Sub
    Dont rely only on your luck. Work hard until You get success.
    vb Code:
    1. Private sub Time_ispassing
    2. While Me.Notgetsuccess
    3. trygain=tryagain+1
    4. Me.workhard
    5. wend
    6. end sub

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How to remove duplicated values from listbox

    vivek_master146 many many thanks to u it worked perfectly!

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to remove duplicated values from listbox

    Generally, I use this
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim i As Long
    3. Dim j As Long
    4.     With List1
    5.         For i = 0 To .ListCount - 1
    6.             For j = .ListCount To (i + 1) Step -1
    7.                 If .List(j) = .List(i) Then
    8.                     .RemoveItem j
    9.                 End If
    10.             Next
    11.         Next
    12.     End With
    13.  
    14. End Sub

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