|
-
May 3rd, 2006, 10:02 PM
#1
Thread Starter
Frenzied Member
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
-
May 4th, 2006, 12:19 AM
#2
Hyperactive Member
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.
-
May 4th, 2006, 02:52 AM
#3
Re: How to remove duplicated values from listbox
 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)
-
May 4th, 2006, 03:01 AM
#4
Re: How to remove duplicated values from listbox
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
-
May 4th, 2006, 05:38 AM
#5
Thread Starter
Frenzied Member
Re: How to remove duplicated values from listbox
 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 !!
-
May 4th, 2006, 05:46 AM
#6
Thread Starter
Frenzied Member
Re: How to remove duplicated values from listbox
 Originally Posted by lintz
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 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.
-
May 4th, 2006, 05:47 AM
#7
Fanatic Member
Re: How to remove duplicated values from listbox
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
Dont rely only on your luck. Work hard until You get success.
vb Code:
Private sub Time_ispassing
While Me.Notgetsuccess
trygain=tryagain+1
Me.workhard
wend
end sub
-
May 4th, 2006, 06:14 AM
#8
Thread Starter
Frenzied Member
Re: How to remove duplicated values from listbox
vivek_master146 many many thanks to u it worked perfectly!
-
May 4th, 2006, 06:16 AM
#9
Re: How to remove duplicated values from listbox
Generally, I use this
VB 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
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
|