Я использовал словарь для сопоставления ключевых элементов clb1 с соответствующими элементами clb2.
Посмотрите на мой пример кода, который работает нормально:
Private mytable As New Generic.Dictionary(Of String, List(Of String))
Sub New()
' Dieser Aufruf ist für den Windows Form-Designer erforderlich.
InitializeComponent()
mytable.Add("Object A", New List(Of String)(New String() {"Object A1", "Object A2", "Object A3"}))
mytable.Add("Object B", New List(Of String)(New String() {"Object B1", "Object B2", "Object B3"}))
mytable.Add("Object C", New List(Of String)(New String() {"Object C1", "Object C2", "Object C3"}))
mytable.Add("Object D", New List(Of String)(New String() {"Object D1", "Object D2", "Object D3"}))
mytable.Add("Object E", New List(Of String)(New String() {"Object E1", "Object E2", "Object E3"}))
For Each key As String In mytable.Keys
Me.clb1.Items.Add(key, False)
Next
End Sub
Private Sub chkSelectAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkSelectAll.CheckedChanged
For i As Int32 = 0 To Me.clb1.Items.Count - 1
Me.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 key As String = DirectCast(Me.clb1.Items(e.Index), String)
If e.NewValue = CheckState.Checked Then
For Each value As String In mytable(key)
Me.Clb2.Items.Add(value, False)
Next
Else
For Each value As String In mytable(key)
Me.Clb2.Items.Remove(value)
Next
End If
End Sub
С уважением,
Тим
EDIT :
Если вы не хотите использовать Generics, вы можете использовать Hashtable и Arraylist.
Мой пример кода почти такой же.
Посмотрите:
Private mytable As New Hashtable()
Sub New()
' Dieser Aufruf ist für den Windows Form-Designer erforderlich.
InitializeComponent()
mytable.Add("Object A", New ArrayList(New String() {"Object A1", "Object A2", "Object A3"}))
mytable.Add("Object B", New ArrayList(New String() {"Object B1", "Object B2", "Object B3"}))
mytable.Add("Object C", New ArrayList(New String() {"Object C1", "Object C2", "Object C3"}))
mytable.Add("Object D", New ArrayList(New String() {"Object D1", "Object D2", "Object D3"}))
mytable.Add("Object E", New ArrayList(New String() {"Object E1", "Object E2", "Object E3"}))
For Each key As String In mytable.Keys
Me.clb1.Items.Add(key, False)
Next
End Sub
Private Sub chkSelectAll_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChkSelectAll.CheckedChanged
For i As Int32 = 0 To Me.clb1.Items.Count - 1
Me.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 key As String = DirectCast(Me.clb1.Items(e.Index), String)
If e.NewValue = CheckState.Checked Then
For Each value As String In mytable(key)
Me.Clb2.Items.Add(value, False)
Next
Else
For Each value As String In mytable(key)
Me.Clb2.Items.Remove(value)
Next
End If
End Sub