完成循环后执行循环

时间:2013-10-07 13:54:53

标签: vb.net visual-studio-2012 for-loop foreach

真的很简单。我希望第二个循环只在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,但是没有找到任何东西。

3 个答案:

答案 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

deletedCountmDeleted.Count相同。

i As INTEGER = c.NAME也没有多大意义。