我很努力想弄清楚如何搜索List(Of Checkbox)以獲取特定的複選框名稱。我想搜索現有列表,並將與特定名稱匹配的複選框添加到另一個列表。在現有的名單,我添加複選框以名字例如:chkClass11,chkClass12,chkClass21等如何正確使用List(Of T).Contains方法?
我的代碼如下:
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,我只是有一個問題填充新列表lstGroupChe ckboxes。新列表的格式有問題嗎?
是否可以向我們展示lstCheckboxes的聲明? – Mr47
你使用什麼版本的.net?爲什麼不使用linq? – linkerro
我已添加您所請求的聲明代碼。我不太清楚如何使用linq。我正在使用VB 2010. – J2Tuner