Reputation: 12002
Using VB 6
In my Project, when I copy the file from one folder to another folder, at the time I want to show the progress bar like copying…., Once the file was copied the Progress bar show’s 100 % Completed.
Code.
'File Copying
Private Sub Copy_Click()
Timer1.Enabled = True
Dim abc As Integer
Dim line As String
abc = FreeFile
Open App.Path & "\DatabasePath.TXT" For Input As #abc
Input #abc, line
databasetext = line
Dim fs As New FileSystemObject, f As File
Set f = fs.GetFile(databasetext)
f.Copy App.Path & "\"
Set fs = Nothing
Close #abc
End Sub
Private Sub Timer1_Timer()
ProgressBar1.Min = 0
ProgressBar1.Max = 100
ProgressBar1.Value = ProgressBar1.Value + 1
If ProgressBar1.Value = ProgressBar1.Max Then
Timer1.Enabled = False
End If
End Sub
Above code Is working, But when I click copy button, Progressbar1 is not displaying, once the file was copied to another folder. Then only progressbar1 is stating.
Both will not working simultaneously.
And Also Once the file was copied, then progress bar should display 100 %. Now it is not displaying correctly, Still the file is copying, Progress bar is showing 100 %
Can any one help to solve the problem.
Need VB 6 Code Help.
Upvotes: 1
Views: 5250
Reputation: 37701
If the standard copy function is blocking the timer from firing then the best thing you can do is write your own copy which reads the source file a few thousand bytes at a time and writes it to the destination file.
Between each read and write operation you need to update your progress bar and (possibly) call DoEvents to make sure it redraws.
Also your timer code makes no sense. It just arbitrarily increases progress every time if fires without reference to how much progress has actually been made. You would be better off passing the progress bar to your copy function so that it can updated as you go.
Something like this would do it:
Private Sub Copy_Click()
Dim abc As Integer
Dim line As String
abc = FreeFile
Open App.Path & "\DatabasePath.TXT" For Input As #abc
Input #abc, line
copyFile line, App.Path & "\" & line, ProgressBar1
Close #abc
End Sub
Sub copyFile(inFile As String, outFile As String, ByRef pg As ProgressBar)
Close
Const chunkSize = 1024
Dim b() As Byte
fhIn = FreeFile
Open inFile For Binary Access Read As #fhIn
fhOut = FreeFile
Open outFile For Binary Access Write As #fhOut
toCopy = LOF(fhIn) 'gets the size of the file
fileSize = toCopy
pb.Min = 0
pb.Max = toCopy
While toCopy > 0
If toCopy > chunkSize Then
ReDim b(1 To chunkSize)
toCopy = toCopy - chunkSize
Else
ReDim b(1 To toCopy)
toCopy = 0
End If
Get #fhIn, , b
Put #fhOut, , b
pg.Value = fileSize - toCopy
DoEvents
Wend
Close #fhIn
Close #fhOut
End Sub
Upvotes: 4
Reputation: 1620
The copy in old school VB6 is a blocking command. So even DoEvents will give the same result (the file will copy, then the progress bar will show up). If you are copying large files over a slow medium and you need to be able to show progress, then you should create the target file and move over bytes in chunks in a loop, in that loop you could update your progress bar. Sadly for the example given in the OP you won't get what you are looking for since every operation is synchronous.
EDIT: Beaten by the guy above me :)
Upvotes: 0
Reputation: 18312
For a progress bar to function, it either has to be updated inline with a periodic loop, or run in a separate thread.
Upvotes: 0