2013-10-07 45 views
1

真的很簡單。我想只有一次關於電子第一已經停止了第二循環開始...完成一個接一個循環

  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上的循環,什麼都沒有,但沒有找到任何東西。

+1

那麼,它會按照你希望的方式工作,一條指令接着另一條指令......究竟是什麼問題? –

+0

這是否不會發生?我的意思是第一個'For'循環會運行,然後第二個'For'會啓動? – user959631

+0

什麼問題?你是否在實際代碼中使用了獨立線程中的這兩個循環? – Tauseef

回答

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也沒有多大意義。

+0

我通過在控件(AllSongsPanel)中的索引來命名控件。不要擔心deletedcontrols var;這是我可以改變一個自定義的trackbar的最大值,我用它作爲一個滾動條 –

+0

剛剛嘗試過,並沒有刪除指定背景的所有控件 –

+0

對不起,我只是一個白癡。我有兩個主要運行相同部分的控件,我正在運行未編輯的控件。我現在要爲每一個人提供一個分支。現在所有的作品。 (之前,它錯過了控件) –

0

由於您沒有關閉任何線程,因此此代碼已經是同步的。你應該已經得到了這個行爲。

+0

我似乎同時做了它們,因爲並不是所有的控件都在它們的名字被改變之前被處置了 –

+1

@ Josh-Mason它以這種方式出現,因爲你過早地退出了循環,因爲你改變了循環中的Controls數組。您刪除的控件越多,錯過的控件越多。 – Plutonix

+0

是的,在思考實際發生的事情後,這很簡單。 –

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 
... 
+0

它工作正常,除了這個問題是關於什麼的,所以我不認爲這會有什麼區別。無論如何,謝謝:) –

+0

對不起,我想我沒有運行我編輯的代碼,而是我運行的是舊代碼:) –

相關問題