tags:

views:

184

answers:

3

Hi, I've got a progressbar in my form that I want to increase the value of in increments based on number of files in a folder.

I guess this is pretty basic, but this kind of programming is pretty new to me.

So I've got these lines of code:

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

I'd appreciate any help at all! :)

Edit: I got it working by doing this (probably a stupid way of doing it though)

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

Edit2: Come to think of it, arrayFiles.length / arrayFiles.length = 1 .. So i could just have typed 1 then.

And, perhaps pretty important, I've set the ProgressBar.Maximum = arrayFiles.Length

A: 

Dont use a for each. Instead if you use an indexed for loop you can do this:

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

(Given that the Value of ProgressBar is a string)

soniiic
Hmm, is couldn't get this to work. However, look at my first post, I managed to get it to work :) Not sure if that's the best way of doing it though..
Kenny Bones
i+1 you should use, doing all 10 files will give i of 9 and count of 10. so never 100%
PoweRoy
You're right, but I just set the minimum value to 0 and the problem was solved :)
Kenny Bones
+1  A: 

You can try something like this using perform step.

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
astander
A: 

Just an aside, since it seems Kenny has solved his issue. Astander's example came from MSDN (link http://msdn.microsoft.com/en-us/library/system.windows.forms.progressbar.aspx) and it is the place I would always suggest you start when researching a problem with .Net. Second, Soniiic the value property takes an integer, so you should definitely not use a string.

Wade73