我使用字典将关键 clb1-items 映射到相应的 clb2-items。看看我运行良好的示例代码:
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
问候,蒂姆
编辑:当你不想使用泛型时,你可以使用 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