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
Printable View
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
If ur list box is getting populated from database then use DISTINCT clause in query to avoid duplicate records.
To avoid duplicates when the listbox is being populated, compare every new item with all the items already present in the list.Quote:
Originally Posted by tony007
This will work....HTH :)
VB Code:
Option Explicit Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _ (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Private Const LB_FINDSTRING = &H18F Private Sub Form_Load() Dim ItemToAdd(3) As String Dim i As Integer ItemToAdd(0) = "Lintz" ItemToAdd(1) = "was" ItemToAdd(2) = "here" ItemToAdd(3) = "Lintz" For i = 0 To 3 If SendMessage(List1.hwnd, LB_FINDSTRING, -1, ByVal ItemToAdd(i)) = -1 Then List1.AddItem ItemToAdd(i) End If Next End Sub
Man my data does not come from db !!Quote:
Originally Posted by noshaba
Man i have 200 items in listbox and u want me to do like this 200 times ?!!!Quote:
Originally Posted by lintz
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.
Here is the perfect code
VB Code:
Private Sub xListKillDupes(listbox As listbox) 'Kills dublicite items in a listbox Dim Search1 As Long Dim Search2 As Long Dim KillDupe As Long KillDupe = 0 For Search1& = 0 To listbox.ListCount - 1 For Search2& = Search1& + 1 To listbox.ListCount - 1 KillDupe = KillDupe + 1 If listbox.List(Search1&) = listbox.List(Search2&) Then listbox.RemoveItem Search2& Search2& = Search2& - 1 End If Next Search2& Next Search1& End Sub Private Sub Command1_Click() Call xListKillDupes(listboxname) End Sub
vivek_master146 many many thanks to u it worked perfectly!
Generally, I use thisVB Code:
Private Sub Command1_Click() Dim i As Long Dim j As Long With List1 For i = 0 To .ListCount - 1 For j = .ListCount To (i + 1) Step -1 If .List(j) = .List(i) Then .RemoveItem j End If Next Next End With End Sub