我正在努力弄清楚如何在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填充它时遇到问题。新列表的格式有问题吗?
答案 0 :(得分:1)
使用.Where()或.Any()代替.Contains()。整个If块:
If lstCheckboxes.Contains(strCheckBoxName) Then
(整个区块,而不仅仅是那条线)可以成为:
lstGroupCheckboxes.AddRange(lstCheckboxes.Where(Function(cb) cb.Name = strCheckBoxName))
用这个替换整个Do ...循环:
lstGroupCheckboxes.AddRange(lstCheckboxes.Where(Function(cb) cb.Name.StartsWith("chkClass" & intGroupNumber))
答案 1 :(得分:0)
这将创建一个匹配的所有复选框的列表:
lstCheckboxes.Where(function(x) x.Name=="chkClass" & intGroupNumber & intClassNumber).ToList()