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
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).
Re: Help with removing post from a ListBox (VB6)
On the second question:
vb Code:
Private Sub cmdRemoveAll()
Dim i as Integer
For i=0 to UBound (lstCombo.ListCount-1)
lstCombo.RemoveItem i
Next i
End Sub
Re: Help with removing post from a ListBox (VB6)
Quote:
Originally Posted by opus
On the second question:
vb Code:
Private Sub cmdRemoveAll()
Dim i as Integer
For i=0 to UBound (lstCombo.ListCount-1)
lstCombo.RemoveItem i
Next i
End Sub
Or lstCombo.Clear?:confused:
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;)