Results 1 to 5 of 5

Thread: Help with removing post from a ListBox (VB6)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Question Help with removing post from a ListBox (VB6)

    Hi,

    I have a project in VB6 where I have a ListBox (lstCombo). I wish to be able to add and remove 4-char long combinations in this ListBox.
    I have a CommandButton (cmdAdd) that looks like this:
    ----------------------------------------
    'Add a combination to the list.
    Private Sub cmdAdd_Click()
    Dim strToAdd As String

    strToAdd = InputBox("Combination to include: ", "Enter Combination")

    If Len(Trim$(strToAdd)) > 0 Then
    If Not ListItemExists(strToAdd) Then
    lstCombo.AddItem strToAdd
    End If
    End If

    End Sub
    ----------------------------------------

    I also have a CommandButton (cmdRemove) that I will be able to remove posts with, but I does not manage to get this to work. My (not so good) try looked like this:
    ----------------------------------------
    'Remove a combination from the list.
    Private Sub cmdRemove_Click()

    Dim strToRemove As String

    strToRemove = InputBox("Combination to remove: ", "Enter Combination")

    If Len(Trim$(strToRemove)) > 0 Then
    If ListItemExists(strToRemove) Then
    lstCombo.RemoveItem strToRemove
    End If
    End If

    End Sub
    ----------------------------------------
    Can you maybe instead do so you click and mark one post and then click “remove”?

    I Would also like to have a CommandButton (cmdRemoveAll) that completely clears the ListBox.

    If anyone has any good ideas on how to solve this in a good way, I would be very thankful!

    /M
    Last edited by mats_1; Mar 29th, 2007 at 02:32 AM.

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Help with removing post from a ListBox (VB6)

    In oder to remove a listitem you need the .ListIndex of this item.
    I guess you could get this information using your Fuction "ListItemExists", which you didn't post, so I'm assuming from here on!
    Right now the Function just gives True or False, in case in gives True it must have loop in some way to the .ListIndex that has the searched Value. Just use a global Var and store this Listindex there for later use.
    your codeline :"stCombo.RemoveIntem strToRemove" should then be changed to "stCombo.RemoveIntem intSavedListIndex" (with intSavedListIndex beeing to saved ListIndex from your Function).
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Help with removing post from a ListBox (VB6)

    On the second question:
    vb Code:
    1. Private Sub cmdRemoveAll()
    2. Dim i as Integer
    3.  
    4. For i=0 to UBound (lstCombo.ListCount-1)
    5.    lstCombo.RemoveItem i
    6. Next i
    7. End Sub
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Help with removing post from a ListBox (VB6)

    Quote Originally Posted by opus
    On the second question:
    vb Code:
    1. Private Sub cmdRemoveAll()
    2. Dim i as Integer
    3.  
    4. For i=0 to UBound (lstCombo.ListCount-1)
    5.    lstCombo.RemoveItem i
    6. Next i
    7. End Sub
    Or lstCombo.Clear?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Posts
    19

    Re: Help with removing post from a ListBox (VB6)

    Thank you for your help, i will try this!
    I now understand that AddItem and RemoveItem does not work entirely the same way

Posting Permissions

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



Click Here to Expand Forum to Full Width