0

我正在努力弄清楚如何在 List(Of Checkbox) 中搜索特定的复选框名称。我想搜索现有列表,并将与特定名称匹配的复选框添加到另一个列表。在现有列表中,我添加了名称为:chkClass11、chkClass12、chkClass21 等的复选框。

我的代码如下:

Dim lstCheckboxes As New List(Of CheckBox)
Dim lstGroupCheckboxes As New List(Of CheckBox)

Do While intNextGroupNumber <= intTotalGroups
    Dim grpNewGroupBox = New GroupBox
    grpNewGroupBox.Name = "grpGroup" & intNextGroupNumber
    grpNewGroupBox.Text = "Group " & intNextGroupNumber
    grpNewGroupBox.Location = New Point(intCurrentXPosition, intCurrentYPosition)
    grpNewGroupBox.Size = New Size(intGroupBoxWidth, intGroupBoxHeight)
    grpNewGroupBox.AutoSize = True
    strGroupBoxNameAddArray(intArrayLocation) = "grpGroup" & intNextGroupNumber

    Do While intCurrentRow < intTotalRows
        Dim chkClassCheckBox = New CheckBox
        chkClassCheckBox.Name = "chkClass" & intNextGroupNumber & intCurrentRow
        chkClassCheckBox.Text = m_DataTableClass.Rows(intCurrentRow).Item("ClassName")
        chkClassCheckBox.Location = New Point(intCurrentXPosition, intCurrentCBYPosition)
        grpNewGroupBox.Controls.Add(chkClassCheckBox)
        lstCheckboxes.Add(chkClassCheckBox)
        intCurrentCBYPosition += 30
        intCurrentRow += 1
        intArrayLength += 1
    Loop

    Me.Controls.Add(grpNewGroupBox)

    strGroupNamesArray(1, intNextGroupNumber - 1) = grpNewGroupBox.Name ' Add group names to array to sort checked checkboxes into

    intCurrentRow = 0
    intNextGroupNumber += 1
    intCurrentGroupBoxHeight = grpNewGroupBox.Height
    intCurrentYPosition = intCurrentYPosition + intCurrentGroupBoxHeight + 50
    intCurrentCBYPosition = 15
    intArrayLocation += 1
Loop

intArrayLocation = 0

'Search strGroupBoxNameArray for next group and then insert strGroupBoxNameAddArray values
For Each Group In strGroupBoxNameAddArray
    intClassNumber = 1
    Try
        strGroupName = strGroupBoxNameAddArray(intArrayLocation)
        intGroupNumber = Microsoft.VisualBasic.Right(strGroupName, 1)

        If Not dicGroups.ContainsKey(Group) Then
            Do
                Try
                    strCheckBoxName = "chkClass" & intGroupNumber & intClassNumber
                    If lstCheckboxes.Contains(strCheckBoxName) Then
                        lstGroupCheckboxes.Add(lstCheckboxes.Item("chkClass" & intGroupNumber & intClassNumber))
                    Else
                        Exit Do
                    End If
                    intClassNumber += 1
                Catch
                    MessageBox.Show(ErrorToString)
                    Exit Do
                End Try
            Loop
            dicGroups.Add(Group, lstGroupCheckboxes)
        End If
        intArrayLocation += 1
    Catch
        MessageBox.Show(ErrorToString)
        Exit For
    End Try
Next

清单上有什么好的参考资料吗?我很难找到能够准确解释列表如何工作的材料。

谢谢!

我确实替换了以下内容:

Do
    Try
        strCheckBoxName = "chkClass" & intGroupNumber & intClassNumber
        If lstCheckboxes.Contains(strCheckBoxName) Then
            lstGroupCheckboxes.Add(lstCheckboxes.Item("chkClass" & intGroupNumber & intClassNumber))
        Else
            Exit Do
        End If
        intClassNumber += 1
    Catch
        MessageBox.Show(ErrorToString)
        Exit Do
    End Try
Loop

具有以下内容:

lstGroupCheckboxes.AddRange(lstCheckboxes.Where(Function(cb) cb.Name.StartsWith("chkClass" & intGroupNumber)))

但是我在这条线上遇到了一些麻烦:

dicGroups.Add(Group, lstGroupCheckboxes)

我用列表 lstCheckboxes 填充这个字典键没有问题,我只是在用新列表 lstGroupCheckboxes 填充它时遇到了问题。新列表的格式有问题吗?

4

2 回答 2

1

使用 .Where() 或 .Any() 而不是 .Contains()。整个 If 块:

If lstCheckboxes.Contains(strCheckBoxName) Then

(整个块,而不仅仅是那一行)可以变成:

lstGroupCheckboxes.AddRange(lstCheckboxes.Where(Function(cb) cb.Name = strCheckBoxName))

用这个替换整个 Do ... Loop:

lstGroupCheckboxes.AddRange(lstCheckboxes.Where(Function(cb) cb.Name.StartsWith("chkClass" & intGroupNumber))
于 2012-12-03T16:14:50.177 回答
0

这将创建一个包含所有匹配复选框的列表:

lstCheckboxes.Where(function(x) x.Name=="chkClass" & intGroupNumber & intClassNumber).ToList()
于 2012-12-03T16:18:21.773 回答