Progressbar value based on number of files
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 / arr开发者_如何学运维ayFiles.length = 1 .. So i could just have typed 1 then.
And, perhaps pretty important, I've set the ProgressBar.Maximum = arrayFiles.Length
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
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)
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.
精彩评论