基于文件数的Progressbar值

时间:2009-11-20 09:50:38

标签: c# vb.net

我的表单中有一个进度条,我希望根据文件夹中的文件数增加增量值。

我想这是非常基本的,但这种编程对我来说很新鲜。

所以我有这些代码:

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

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

不要为每个人使用a。相反,如果您使用索引for循环,则可以执行此操作:

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

(假设Value ProgressBar是一个字符串)

答案 2 :(得分:0)

撇开,因为肯尼似乎已经解决了他的问题。 Astander的例子来自MSDN(链接http://msdn.microsoft.com/en-us/library/system.windows.forms.progressbar.aspx),这是我一直建议你在研究.Net问题时开始的地方。其次,Soniiic值属性采用整数,所以你绝对不应该使用字符串。