Checklistbox.RemoveAt unchecks next item in listbox

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Heres some test code I used. Just a checklistbox on the form with this code-behind:
<pre lang="x-vbnet Dim list As New List(Of String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i = 0 To 10
list.Add("Test " + i.ToString)
Next

For i = 0 To list.Count - 1
CheckedListBox1.Items.Add(list.Item(i))
Next

End Sub

Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
If e.NewValue <> CheckState.Checked Then
CheckedListBox1.Items.RemoveAt(e.Index)
list.RemoveAt(e.Index)
End If
End Sub[/code]
<br/>

<br/>
I use a list to keep track of the items in the listbox, so if I remove one of those items from the listbox I want to also remove it from the list using the same index.
If you use this code and load the form and then check all the items, youll notice that when you uncheck one it will remove the item and THEN also uncheck the next item...why is that?
<br/>
<br/>
<br/>

View the full article
 
Back
Top