用另一个(checkedlistbox)填充一个checkedlistbox

时间:2010-03-24 18:25:16

标签: vb.net checkbox checkedlistbox populate

我在根据另一个选项中的选择填充checkedlistbox(CLB)时遇到了困难。还应注意,我在顶部有一个“全选”复选框,用于检查/取消选中第一个CLB中的所有项目。这是代码:

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
    If chkSelectAll.Checked = False Then
        clb2.Items.Clear()
    End If
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.Items.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

第一个CLB在按钮点击事件中填充了值的arraylist。基于在第一个CLB中检查的内容,来自结构的arraylist的相应值应该填充第二个CLB。以下代码部分有效,直到单击“全选”复选框,此时如果在选中“全选”之前选择了其他值,则第二个CLB填充正确数量的相应值但仅限于最近的那些值选择第一个CLB的项目,而不是所有尚未选择的项目的所有相应值。

非常感谢任何见解。

~8th

1 个答案:

答案 0 :(得分:0)

我使用了一个Dictionary将关键的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