1

这真的很简单。我希望第二个循环仅在 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

我对此并没有太多的想法,我在谷歌上也找不到任何问题。我的想法可能会比需要的更广泛,所以我想先检查是否有更简单的解决方案。我已经查看了 msdn 上的 For Loops 等等,但没有找到任何东西。

4

3 回答 3

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
...
于 2013-10-07T14:06:09.573 回答
0

由于您没有线程化任何东西,因此此代码已经是同步的。你应该已经得到了这种行为。

于 2013-10-07T13:57:57.290 回答
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也没有多大意义。

于 2013-10-07T14:08:18.980 回答