populating one checkedlistbox with another (checkedlistbox)
Posted
by 8thWonder
on Stack Overflow
See other posts from Stack Overflow
or by 8thWonder
Published on 2010-03-24T18:25:15Z
Indexed on
2010/03/24
18:53 UTC
Read the original article
Hit count: 318
I am having difficulties populating a checkedlistbox (CLB) based on the selection(s) made in another. It should also be noted that I have a "Select All" checkbox at the top that checks/unchecks all of the items in the first CLB. Here's the code:
Private Sub chkSelectAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSelectAll.CheckedChanged
For i As Integer = 0 To clb1.Items.Count - 1
clb1.SetItemChecked(i, chkSelectAll.Checked)
Next
End Sub
Private Sub clb1_ItemCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles clb1.ItemCheck
Dim i As Integer = clb1.SelectedIndex
For j As Integer = 0 To al_2.Count - 1
If i = -1 Then
For k As Integer = 0 To al_2.Count - 1
If Not clb2.Contains(al_2(k).sDate) Then
clb2.Items.Add(al_2(k).sDate)
Else : k += 1
End If
Next
ElseIf (e.NewValue = CheckState.Checked And al_2(j).sName = al_1(i)) Then
clb2.Items.Add(al_2(j).sDate)
ElseIf (e.NewValue = CheckState.Unchecked And al_2(j).sName = al_1(i)) Then
clbProdBkups.Items.Remove(al_2(j).sDate)
End If
Next
End Sub
The first CLB is populated with an arraylist of values on the button click event. Based on whatever is checked in the first CLB, corresponding values from an arraylist of structures should fill the second CLB. The following code partially works until the "Select All" checkbox is clicked at which point one of two things happens:
If other values have been selected before "Select All" is checked, the second CLB is filled with the correct number of corresponding values BUT only those of the most recently selected item of the first CLB instead of all of corresponding values of all of the items that were not already selected. When "Select All" is unchecked, the most recently incorrect values are removed, everything in CLB 1 is unchecked but the values in CLB 2 that were selected before "Select All" was checked remain.
If "Select All" is checked before anything else is selected, I get an "unable to cast object of type 'System.String' to type 'System.Windows.Forms.Control'" error that points to the following statement from the itemcheck event:
If Not clb2.Contains(al_2(k).sDate) Then
Any insights will be greatly appreciated.
~8th
© Stack Overflow or respective owner