进度栏和文件复制问题? [英] Progress Bar and File Copying Problem?

查看:152
本文介绍了进度栏和文件复制问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用VB 6



在我的项目中,当我将文件从一个文件夹复制到另一个文件夹时,我想显示进度条, 。文件复制后,进度条显示的完成百分比为100%。



代码。



  Private Sub Copy_Click()
Timer1.Enabled = True
Dim abc As Integer
Dim line As String
abc = FreeFile
打开App.Path& \DatabasePath.TXT对于输入为#abc
输入#abc,行
databasetext =行
Dim fs作为新FileSystemObject,f As文件
设置f = fs。 GetFile(databasetext)
f.Copy App.Path& \
设置fs =无
关闭#abc
结束子

私有子Timer1_Timer()
ProgressBar1.Min = 0
ProgressBar1.Max = 100
ProgressBar1.Value = ProgressBar1.Value + 1
如果ProgressBar1.Value = ProgressBar1.Max然后
Timer1.Enabled = False
结束如果
End Sub

上面的代码工作,但是当我点击复制按钮时,Progressbar1不显示该文件被复制到另一个文件夹。



此外,一旦文件被复制,那么进度条应该显示100%。现在无法正确显示,仍然是文件正在复制,进度栏显示为100%



任何人都可以帮助解决问题。



需要VB 6代码帮助。

解决方案

如果标准复制功能阻止定时器触发那么你可以做的最好的事情是写自己的副本,每次读取源文件几千个字节,并将其写入目标文件。



和写操作你需要更新你的进度条,并(可能)调用DoEvents,以确保它重绘。



还有你的计时器代码没有意义。它只是任意地增加进度每次,如果发生,而不参考实际进行了多少进展。



这样做可以做到:

  Private Sub Copy_Click()
Dim abc As Integer
Dim line As String
abc = FreeFile
打开App.Path& \DatabasePath.TXT对于输入为#abc
输入#abc,行
copyFile行,App.Path& \& line,ProgressBar1
关闭#abc
结束子



子复制文件(inFile As String,outFile As String,ByRef pg As ProgressBar)

关闭

Const chunkSize = 1024
Dim b()As Byte

fhIn = FreeFile

打开inFile二进制访问读为#fhIn

fhOut = FreeFile

打开outFile对于二进制访问写为#fhOut

toCopy = LOF(fhIn)'获取文件的大小
fileSize = toCopy

pb.Min = 0
pb.Max = toCopy


$ b b
当toCopy> 0
如果toCopy> chunkSize then
ReDim b(1 To chunkSize)
toCopy = toCopy - chunkSize
Else
ReDim b(1 To to copy)
toCopy = 0
如果

获取#fhIn,,b
输入#fhOut,,b

pg.Value = fileSize - toCopy
DoEvents
Wend
关闭#fhIn
关闭#fhOut
结束子


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.

解决方案

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

这篇关于进度栏和文件复制问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆