0

我在根据另一个中所做的选择填充选中列表框 (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 使用按钮单击事件上的值数组列表填充。根据在第一个 CLB 中检查的内容,来自结构数组列表的相应值应填充第二个 CLB。以下代码部分有效,直到单击“全选”复选框,此时如果在选中“全选”之前已选择其他值,则第二个 CLB 填充正确数量的对应值,但仅填充最近的值选择第一个 CLB 的项目,而不是所有尚未选择的项目的所有对应值。

任何见解将不胜感激。

~8th

4

1 回答 1

0

我使用字典将关键 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
于 2010-03-24T22:08:15.620 回答