2009-11-20 88 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,问题解决了:) –