真的很简单。我希望第二个循环只在e上的第一个循环停止后启动...
Dim i As Integer
For Each c As Control In AllSongsPanel.Controls
If c.BackColor = Color.FromArgb(30, 30, 30) Then
My.Computer.FileSystem.DeleteFile(c.Tag)
i = c.Name
c.Dispose()
deletedCount = deletedCount + 1
End If
Next
itemCount = 0
For Each c As Control In AllSongsPanel.Controls
If c.Width = AllSongsPanel.Width - 23 Then
itemCount = itemCount + 1
c.Name = itemCount
End If
Next
我对此并没有太多想法,我也无法在Google上找到任何问题。我的想法可能比需要的更广泛,所以我想先检查一下是否有更简单的解决方案。 我在msdn上看过For Loops,但是没有找到任何东西。
答案 0 :(得分:0)
由于您没有删除任何内容,因此此代码已经是同步的。你应该已经得到了这种行为。
答案 1 :(得分:0)
当您在此列表中的for..each循环内时,无法从列表中删除项目。我首先要记住所有要删除的项目并在之后有效删除它们。像这样:
Dim i as integer
Dim deleteControls As New List(Of Control)
For Each c As Control In AllSongsPanel.Controls
If c.BackColor = Color.FromArgb(30, 30, 30) Then
deleteControls.Add(c)
End If
Next
For Each c As Control In deleteControls
My.Computer.FileSystem.DeleteFile(c.Tag)
i = c.Name
AllSongsPanel.Controls.Remove(c)
deletedCount = deletedCount + 1
Next
...
答案 2 :(得分:0)
更改删除控件的方式。
Dim mDeleted as new List(of Control)
Dim i As Integer
For Each c As Control In AllSongsPanel.Controls
If c.BackColor = Color.FromArgb(30, 30, 30) Then
My.Computer.FileSystem.DeleteFile(c.Tag)
i = c.Name
mDeleted.Add(c)
' no longer needed (?)
'deletedCount = deletedCount + 1
End If
Next
For each C as control in mDeleted
' i think this is the right syntax:
AllSongsPanel.Controls.Remove(c)
Next
' add the second loop...not sure what it is doing
deletedCount
与mDeleted.Count
相同。
i As INTEGER = c.NAME
也没有多大意义。