Helpful Information
 
 
Category: Visual Basic Programming
Error with command button

I have 2 command buttons 1 that add's to a list that goes like:


Private Sub Command1_Click()
lstwindows4.AddItem Text1.Text
Text1.Text = ""
End Sub


Which works fine and does what i want - but I have another command button which when clicked should delete an entry from the list if selected:


Private Sub Command2_Click()

ty = lstwindows4.ListCount
For ber = 0 To ty - 1
lstwindows4.ListIndex = ber
If lstwindows4.Text = Text2.Text Then
lstwindows4.RemoveItem ber
Text2.Text = ""
Exit Sub
End If
Next ber
Text2.Text = ""
End Sub


it does not seem to delete from the list?

any help is appreciated

:)

Try something like this:

cnt = lstListName.ListCount
For cntr = 0 To lstListName.ListCount - 1
If txtTextField = lstListName.List(cntr) Then
lstListName.RemoveItem cntr
txtTextField = vbNullString
Exit Sub
End If
Next
This will effectively remove an entry from a listbox where it matches the text field value.










privacy (GDPR)