...

View Full Version : Need help With a script that removes items from listboxes



Bry Man
12-21-2004, 03:53 AM
Hey, sorry for the title, I wasnt sure how long It could be and what I wasnt completely sure how to ask my question so Ill restate it here.

I found a script on the net that supposedly lets you remove a selected item in a list box by selecting it and hitting a button. The only thing is I cant seem to get it to work I tried drawing out a list box and naming is ListView1 but that isnt right, can anyone help me figure out how im supposed to name the objects to make this work?


Private Sub Form_Load()
ListView1.ListItems.Add , , "TEST"
ListView1.ListItems.Add , , "TEST2"
End Sub

Private Sub cmdButton_Click()
Dim item As ListItem
Dim i As Integer

For i = 1 To ListView1.ListItems.Count
If (ListView1.ListItems(i).Selected) Then
ListView1.ListItems.Remove (i)
Exit For
End If
Next i
End Sub

Brandoe85
12-21-2004, 08:28 AM
You doing this in .NET? If so, this is how I would do it,
Form load:


ListView1.Items.Add("Test")
ListView1.Items.Add("Test2")

Button:


Dim i As Integer
For i = 0 To ListView1.Items.Count - 1
If ListView1.GetSelected(i) Then
ListView1.Items.RemoveAt(i)
End If
Next

Bry Man
12-21-2004, 08:41 AM
Well I thought it was on a regular vb forum, and the code looked like it was regular vb so taht what I was attempting it in. However I have no idea how to utilize it because it would always give me an error.

Brandoe85
12-21-2004, 08:44 AM
Well in vb6 put what I posted for the form load in my first post. Then change the button code to this:


Dim i As Integer
For i = 0 To ListView1.ListCount - 1
If ListView1.Selected(i) Then
ListView1.RemoveItem i
End If
Next

Bry Man
12-21-2004, 08:47 AM
I dont have to do anything specail as far as renaming the list box or anything right, I dont really need the items added at form load because Im loading the list content froma text file.

EDIT: When I add that code above, the last code, it says that the ListView1 is not defined

Brandoe85
12-21-2004, 08:50 AM
Thats what I named the list box. Did you name your list box ListView1?

Bry Man
12-21-2004, 08:53 AM
Ah, that was the problem, I tried that before but it didnt work, the new code you gave me did the trick thank you for your help =)

Brandoe85
12-21-2004, 08:55 AM
You're welcome :thumbsup: that code you first posted was cookey(or i've just never seen any like it) :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum