progress bar problemo

VBnetster

Active member
Joined
Feb 15, 2008
Messages
32
Programming Experience
Beginner
hi!

Just playing around with the progress bar trying to figure it out.. doesnt seem as simple as VB6.

I have this code:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dirinfo As New DirectoryInfo("C:\WINDOWS.0\system32\")
        Dim i As Integer = 0

        For Each fi In dirinfo.GetFiles
            i += 1
            Proc.Maximum = i
            ListBox1.Items.Add(fi.Name.ToString)
            File.Copy(String.Format("C:\WINDOWS.0\system32\{0}", fi.Name.ToString), _
                      String.Format("C:\temp\{0}", fi.Name.ToString))
            If Proc.Value < Proc.Maximum Then
                Proc.Value += 1
            End If
        Next

    End Sub


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Proc.Minimum = 0
        Proc.Step = 1
        Proc.Value = 0
    End Sub

in theory the progress bar should step up 1 after each file is copiedbut the progress bar seems to hit 100% before the files have even finished copying.

some code such as "string.format" may seem un-needed for the task but i'm using it just to learn.
 
I think the problem here is that the progress bar doesn't have a defined maximum. The following code appears to work:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim dirinfo As New DirectoryInfo("C:\")
            Dim i As Integer = 0
            Dim fi As FileInfo

            prgTest.Maximum = dirinfo.GetFiles.Length
            For Each fi In dirinfo.GetFiles
                i += 1
                ListBox1.Items.Add(fi.Name.ToString)
                ListBox1.Update()
                System.Threading.Thread.Sleep(100)
                prgTest.Value = i
            Next

        Catch ex As Exception
            MessageBox.Show("Error in Progressbar Test", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
        End Try
    End Sub
 
VB.NET:
prgTest.Maximum = dirinfo.GetFiles.Length
            For Each fi In dirinfo.GetFiles
Why throw away processing cycles getting files twice when you only need to get them once? The GetFiles method returns a string array which is not difficult to catch:
VB.NET:
dim files() as string=getfiles
prog.max=files.length
for each file in files...
 
Back
Top