Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Sep 2002
    Location
    California
    Posts
    511
    Thanks
    1
    Thanked 1 Time in 1 Post

    Need help With a script that removes items from listboxes

    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?

    Code:
    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
    When traveling down the road of life, Try not to drag your face in the gravel.

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You doing this in .NET? If so, this is how I would do it,
    Form load:
    Code:
    ListView1.Items.Add("Test")
    ListView1.Items.Add("Test2")
    Button:
    Code:
    Dim i As Integer
             For i = 0 To ListView1.Items.Count - 1
    	       If ListView1.GetSelected(i) Then
    		      ListView1.Items.RemoveAt(i)
    	       End If
             Next

  • #3
    Regular Coder
    Join Date
    Sep 2002
    Location
    California
    Posts
    511
    Thanks
    1
    Thanked 1 Time in 1 Post
    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.
    When traveling down the road of life, Try not to drag your face in the gravel.

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Well in vb6 put what I posted for the form load in my first post. Then change the button code to this:
    Code:
    Dim i As Integer
         For i = 0 To ListView1.ListCount - 1
              If ListView1.Selected(i) Then
                   ListView1.RemoveItem i
              End If
         Next

  • #5
    Regular Coder
    Join Date
    Sep 2002
    Location
    California
    Posts
    511
    Thanks
    1
    Thanked 1 Time in 1 Post
    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
    Last edited by Bry Man; 12-21-2004 at 08:49 AM.
    When traveling down the road of life, Try not to drag your face in the gravel.

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Thats what I named the list box. Did you name your list box ListView1?

  • #7
    Regular Coder
    Join Date
    Sep 2002
    Location
    California
    Posts
    511
    Thanks
    1
    Thanked 1 Time in 1 Post
    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 =)
    When traveling down the road of life, Try not to drag your face in the gravel.

  • #8
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You're welcome that code you first posted was cookey(or i've just never seen any like it)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •