removing items from a list box based on the individual items content

hello

i am trying to remove items from a list box based on if they contain the text acknowledged or not.

If i acknowledge 1 item , it appends the word acknowledge to the end of the currently selected item in the listbox, then if i click my clear button the item in the list box will go using removeat key word. however, if i click the button acknowledge all then this adds the string acknowledge to the end of all items in the list box

my problem is if i then select the clear button only half the items will be gone even thoguh all items show has having the word acknowledge appended to them in the list box.

its an alarm list. here is the code, i think its close. many thanks in advance

Dim AcknowledgedAsString

Dim iAsInteger

Try

If ListBox1.SelectedIndex >= -1Then

For i = 0To ListBox1.Items.Count

ListBox1.SelectedItem = i

ListBox1.SelectedItem.Equals(i)

Acknowledged = ListBox1.GetItemText(ListBox1.SelectedItem)

If AcknowledgedLike"*Acknowledged*"Then

ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)

Else

Beep()

EndIf

Next i

EndIf

Catch

MsgBox("error with sub procedure")

EndTry.

[2753 byte] By [igibbwiz] at [2007-12-21]
# 1

Try iterating backwards through the collection

For i as integer = Listbox1.items.count to 0 step -1

DMan1 at 2007-9-10 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 2

hello

thank you sir, i did what you said and it works well. Not sure i understand why but ill keep at it. Hers what i ended up with.

again many thanks, ian

Try

If ListBox1.SelectedIndex >= -1 Then

For i = ListBox1.Items.Count To 0 Step -1

ListBox1.SelectedIndex = i - 1

Acknowledged = ListBox1.GetItemText(ListBox1.SelectedItem)

If Acknowledged Like "*Acknowledged*" Then

ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)

Else

Beep()

End If

Next i

End If

Catch

MsgBox("error with sub procedure")

End Try

igibbwiz at 2007-9-10 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...
# 3

Because the count is evaluated each time through the loop, Going forward through the loop causes you to only see half of the items when you delete them....ie

For i = 0 to Items.Count

Items.Delete(x)

Next

If we start with 10 items the first time through it deletes item 0 ....the next time through the loop i = 1 but count now equals 9 by the time you are 50% through the loop you have i = 50% of the count or in this case i = 5 and count = 5

DMan1 at 2007-9-10 > top of Msdn Tech,Visual Studio Express Editions,Visual Basic 2005 Express Edition...