Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ListBox Remove Item 2

Status
Not open for further replies.

Vec

IS-IT--Management
Jan 29, 2002
418
0
0
US
Using a command button, how do I remove the selected item from a ListBox? "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-- Rich Cook.

teklogo.gif

 
Try using this code:

Private Sub Command1_Click()
Dim intX As Integer

intX = List1.ListIndex
List1.RemoveItem (intX)

End Sub
 
Thanks abrewis, that did it.
Question 2 is how do I parse the ListBox and remove duplicate values?
Is this hard? "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-- Rich Cook.

teklogo.gif

 
I would reccomend that you loop through each value in the listbox and use the StrComp function to compare the each of the values, and remove and of the duplicates.

Something like this:

Dim intX As Integer
Dim intY As Integer

For intX = 0 To List1.ListCount
For intY = intX + 1 To List1.ListCount - 1
If StrComp(List1.List(intX), List1.List(intY), vbTextCompare) = 0 Then '### Remove Duplicate
Next intY
Next intX
 
abrewis,
Tried:
Dim intX As Integer
Dim intY As Integer

For intX = 0 To List1.ListCount
For intY = intX + 1 To List1.ListCount - 1
If StrComp(List1.List(intX), List1.List(intY), vbTextCompare) = 0 Then '### Remove Duplicate
Next intY
Next intX

Gives the error "Next without for"
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-- Rich Cook.

teklogo.gif

 
That'll be because you need to add your code to remove the duplicate entry or call a sub procedure to do this at the '### Remove Duplicate' point.

Good luck
 
Here is what I have tried, and still recoeve the same error:

Dim intX As Integer
Dim intY As Integer
Dim intZ As Integer
intZ = List1.ListIndex

For intX = 0 To List1.ListCount
For intY = intX + 1 To List1.ListCount - 1
If StrComp(List1.List(intX), List1.List(intY), vbTextCompare) = 0 Then
List1.RemoveItem (intZ)
Next intY
Next intX "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-- Rich Cook.

teklogo.gif

 
Try this, it works OK from here:

Dim intX As Integer
Dim intY As Integer

For intX = 0 To List1.ListCount
For intY = intX + 1 To List1.ListCount - 1
If StrComp(List1.List(intX), List1.List(intY), vbTextCompare) = 0 Then
List1.RemoveItem (intY)
End If
Next intY
Next intX
 
I see what I did wrong, thanks! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-- Rich Cook.

teklogo.gif

 
abrewis, there is a bug in the procedure.

Put the following in the list box
aaaa
aaaa
aaaa
aaaa
bbbb
bbbb
bbbb
bbbb

run the code to remove duplicates and notice that you have to run the code sevberal times to remove all duplicates??? "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-- Rich Cook.

teklogo.gif

 
Very true, I'm afraid I put this code together rather too quickly this morning and therefore they I cannot guarantee that it's totally 'bug free'. However this problem can be solved by changing the forth line to

For intY = intX To List1.ListCount - 1

Cheers
 
No dice, try using these in the list:

me@email.com
me@email.com
me@email.com
me@email.com
me@email.com
me@email.com
me@email.com
you@email.com
you@email.com
you@email.com
you@email.com
you@email.com
you@email.com
you@email.com

notice it still leaves duplicates "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-- Rich Cook.

teklogo.gif

 
Which has the problem that it removes singletons. The trick is to walk backwards through the list rather than forwards (mainly because you then don't have to deal with the problem that removing an item mucks up the indexes):

Dim intX As Integer
Dim intY As Integer

For intX = List1.ListCount To 0 Step -1
For intY = intX - 1 To 0 Step -1
If StrComp(List1.List(intX), List1.List(intY), vbTextCompare) = 0 Then
List1.RemoveItem (intY)
End If
Next intY
Next intX
 
Thank you kindly to both of you. And thank you for explaining why! I am not just one of these code pasters, I like to know why things work.
Even though I own and manage a technology company, I am far from the worlds greatest programmer. I took Visual Basic Comp 104 in a college course which is an intro and then a basic vb foundation. This is what has created my hunger for vb. I have programmers that work for me, but I won't ask them for these answers, what would they think if the boss didn't know! They all look up to me and work hard for me because they think I am some sort of vb god. It creates quality work because they won't cut corners in fear that the boss might notice.
Anyway, thanks again to you both, this is why I come here!
See you for my next problem which won't be long! "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
-- Rich Cook.

teklogo.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top