I have a CheckedListBox called fields, and a ListBox called included, and an array called fieldsArry.
Here is the code when the user checks a box:
Private Sub fields_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles fields.ItemCheck 'We do not want to call the updateFieldArray function when it is just loading up the fields 'because those will already be in our array, but we want to call it all other times. If reload = "No" Then updateFieldArray() End If reload = "No" End Sub
Private Sub updateFieldArray() Dim i As Integer = 0 While i < fields.Items.Count If fields.CheckedItems.Contains(fields.Items.Item(i)) Or fields.SelectedItem = fields.Items.Item(i) Then If Not fieldsArry.Contains(fields.Items.Item(i)) Then fieldsArry.Add(fields.Items.Item(i)) included.Items.Add(fields.Items.Item(i)) End If ElseIf Not fields.CheckedItems.Contains(fields.Items.Item(i)) Then If fieldsArry.Contains(fields.Items.Item(i)) Then fieldsArry.Remove(fields.Items.Item(i)) included.Items.Remove(fields.Items.Item(i)) End If End If i = i + 1 End While End Sub
I know why it is only working on one and not the other - my problem is figuring out how to get it to work on both. For whatever reason (this drives me crazy sometimes) when I check a box in the CheckedListBox it runs all the code I put in there before it even checks the box. So I can't just reference that box as a checked box until after all the code in the itemCheck event is finished. I remedied this in the updateFieldArray by including the currently selected item as a included item, which means that even when they are trying to uncheck something (remove it from the array) it is selected and will be included.
I'm afraid that I am getting to rambly, so if there are any questions then I would be happy to clear them up for you.