2009-11-20 91 views
0

我在表單中有一個進度條,我想根據文件夾中的文件數增加值的增量。基於文件數的進度條值

我想這是非常基本的,但這種編程對我來說是非常新的。

所以我有幾行代碼:

Dim directory As New IO.DirectoryInfo("C:\Temp") 
Dim arrayFiles as IO.FileInfo() = directory.GetFiles("*.txt") 
Dim fi As IO.FileInfo 

For Each fi In arrayFiles 

    Do stuff 
    ProgressBar.Value = "something" 
Next 

我會很感激的任何幫助都! :)

編輯:我把它做這個(雖然這樣做大概一個笨方法)工作

For Each fi In arrayFiles 
    ProgressBar.Value = ProgressBar.Value + arrayFiles.Length/arrayFiles.Length 
Next 

EDIT2:試想想它,arrayFiles.length/arrayFiles.length = 1。 所以我只能輸入1。

而且,也許是非常重要的,我給自己定了ProgressBar.Maximum = arrayFiles.Length

回答

1

你可以嘗試這樣的事情使用執行步驟。

Private Sub CopyWithProgress(ByVal ParamArray filenames As String()) 
    ' Display the ProgressBar control. 
    pBar1.Visible = True 
    ' Set Minimum to 1 to represent the first file being copied. 
    pBar1.Minimum = 1 
    ' Set Maximum to the total number of files to copy. 
    pBar1.Maximum = filenames.Length 
    ' Set the initial value of the ProgressBar. 
    pBar1.Value = 1 
    ' Set the Step property to a value of 1 to represent each file being copied. 
    pBar1.Step = 1 

    ' Loop through all files to copy. 
    Dim x As Integer 
    for x = 1 To filenames.Length - 1 
     ' Copy the file and increment the ProgressBar if successful. 
     If CopyFile(filenames(x - 1)) = True Then 
      ' Perform the increment on the ProgressBar. 
      pBar1.PerformStep() 
     End If 
    Next x 
End Sub 
0

不要使用爲每個。相反,如果你使用一個索引for循環,你可以這樣做:

ProgressBar.Value = (i/arrayFiles.Count) * 100 + "%" 

(假設的ProgressBarValue是一個字符串)

+0

嗯,是不能得到這個工作。然而,看看我的第一篇文章,我設法讓它工作:)不知道這是否是最好的方式,雖然.. –

+0

我應該使用1,做所有10個文件將給我的9和計數10.所以從來沒有100% – RvdK

+0

你是對的,但我只是將最小值設置爲0,問題解決了:) –