Results 1 to 16 of 16

Thread: [RESOLVED] Showing copying progress in a Label

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    84

    Resolved [RESOLVED] Showing copying progress in a Label

    Hey,

    I'm making an application that copies files from one folder to another.
    I want the copying progress shown in a label, just like in DOS when you copy files you'll see something like this:

    folder\link.url
    folder\Readme.txt
    folder\setup.exe
    folder\site.htm
    4 file(s) copied.

    I've come this far:

    VB Code:
    1. Option Explicit
    2. Dim a As Integer
    3. Dim b As Integer
    4. Dim Destination As String
    5.  
    6. Private Sub Command1_Click()
    7.     Destination = "C:\Temp-1"
    8.  
    9.     b = File1.ListCount - 1
    10.    
    11.     For a = 0 To b
    12.     Label1.Caption = Label1.Caption & vbCrLf & "Copying: " & File1.List(a)
    13.     FileCopy File1.List(a), Destination & "\" & File1.List(a)
    14.     Next
    15. End Sub

    This works fine, yes, but now the text is shown in Label1 after all files have been copied. So my question is:
    How can I make it show the current file that is being copied? Like this:

    (When it's copying file1.txt):
    Copying: file1.txt
    (When file1.txt is copied and it's copying file2.txt):
    Copying: file2.txt

    If I didn't explain clear enough, just ask

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Showing copying progress in a Label

    Welcome to the Forum rade

    You probably need a DoEvents,try:
    VB Code:
    1. Option Explicit
    2. Dim a As Integer
    3. Dim b As Integer
    4. Dim Destination As String
    5.  
    6. Private Sub Command1_Click()
    7.     Destination = "C:\Temp-1"
    8.  
    9.     b = File1.ListCount - 1
    10.    
    11.     For a = 0 To b
    12.         DoEvents    '<<<<<<<<<<< Added
    13.         Label1.Caption = Label1.Caption & vbCrLf & "Copying: " & File1.List(a)
    14.         FileCopy File1.List(a), Destination & "\" & File1.List(a)
    15.     Next
    16. End Sub
    This needs to be optimizd too.
    Last edited by Bruce Fox; Mar 24th, 2006 at 05:42 AM.

  3. #3
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Showing copying progress in a Label

    Now, to otimize (you may need to playwith the value), try:
    VB Code:
    1. If a Mod 2 = 0 Then DoEvents
    Note: Change the Value of 2 to suit (Eg 5)

  4. #4
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    Re: Showing copying progress in a Label

    DoEvents can slow the process down, and if you use Bruce's suggestion to call DoEvents just once every x times, the label will not be refreshed every time.

    If you just want the label to be repainted, use the refresh method of the label. This will also slow it down a bit, but not as much as DoEvents can do.

    VB Code:
    1. Option Explicit
    2. Dim a As Integer
    3. Dim b As Integer
    4. Dim Destination As String
    5.  
    6. Private Sub Command1_Click()
    7.     Destination = "C:\Temp-1"
    8.  
    9.     b = File1.ListCount - 1
    10.    
    11.     For a = 0 To b
    12.         Label1.Caption = Label1.Caption & vbCrLf & "Copying: " & File1.List(a)
    13.         Label1.Refresh
    14.         FileCopy File1.List(a), Destination & "\" & File1.List(a)
    15.     Next
    16. End Sub

    EDIT: and if you want to use DoEvents, you should call it after the label's caption is changed, and not before. Otherwise the caption is displayed after the file is copied.
    Frans

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    84

    Thumbs up Re: Showing copying progress in a Label

    Thank you both, Bruce and Frans!
    Both ways work fine. I still prefer the label-refresh method I'm more familiar with it.
    Now I still have one little probelm:
    How do I add a scrollbar to the label? (Please don't tell it isn't possible )
    "Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies."
    - Linus Torvalds

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    84

    Re: Showing copying progress in a Label

    Quote Originally Posted by Bruce Fox
    Welcome to the Forum rade
    Thanks Bruce These forums seem really good and useful.

    -rade
    "Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies."
    - Linus Torvalds

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: Showing copying progress in a Label

    FransC is right, there is a place for .Refresh. There is also a place for DoEvents...
    especially when you may want to abort a Loop with the click of a button - that is when it is helpful.

    In any case, you have what you need, good luck with your app, and hope to see you as a regular here

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    84

    Re: Showing copying progress in a Label

    Thanks again Bruce But I think you missed my little question there:

    How do I add a scrollbar to the label? (Please don't tell it isn't possible )
    "Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies."
    - Linus Torvalds

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Showing copying progress in a Label

    Quote Originally Posted by rade
    How do I add a scrollbar to the label? (Please don't tell it isn't possible )
    I'm being completely serious. It isn't possible.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    84

    Re: Showing copying progress in a Label

    Quote Originally Posted by Hack
    I'm being completely serious. It isn't possible.

    Well.. Any suggestions how I could do this? The largest amout of files that will be copied is between 10 and 15, but the label the progress is shown on only has place for 7 rows. Yes, it would be easiest to make the label higher, but then it would be all too big.

    Can you come up with any way of "autoscrolling" the label or something similar? Or maybe there is another object, in which it is possible to include a scrollbar, that I could use for this instead of a label?
    Any help is highly approcieted

    rade
    Last edited by rade; Mar 24th, 2006 at 07:22 AM. Reason: Typo
    "Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies."
    - Linus Torvalds

  11. #11
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Showing copying progress in a Label

    You cans use a text box instead of a label, and just disable the input for it.
    Or, use a listbox to show the "history" of what you are doing.

  12. #12
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Showing copying progress in a Label

    The Label control is one of the most difficult controls to manipulate because it does not have a hWnd property. The functionality of a label, as it is, is what you get.

    There have been a number of times when I needed more functionality that I knew I could get out of a label so I used a textbox (which does have an hWnd property). I set the textbox borderstyle to None, and its backcolor to the same backcolor as my form. This gave it the "look" of a label. But, because it was actually a textbox, I could do things with it that I couldn't with a label.

    Like add scrollbars.

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    84

    Re: Showing copying progress in a Label

    Alright! Thanks a lot guys, this was really really helpful! Man I love these forums

    I changed the label to a textbox => Problem Resolved
    "Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies."
    - Linus Torvalds

  14. #14
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Showing copying progress in a Label

    Quote Originally Posted by rade
    Alright! Thanks a lot guys, this was really really helpful! Man I love these forums

    I changed the label to a textbox => Problem Resolved
    A couple of additional things that you will want to do.

    Set the Locked property of the textbox to True. That way, like a label, you can't actually type in it. Also, you don't want the blinking cursor to show when the textbox has focus, so use this API to hide it.
    VB Code:
    1. Private Declare Function HideCaret Lib "User32" (ByVal hWnd As Long) As Long
    2.  
    3. Private Sub Text1_GotFocus()
    4. HideCaret Text1.hWnd
    5. End Sub

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Location
    Finland
    Posts
    84

    Re: [RESOLVED] Showing copying progress in a Label

    Okay thanks again
    "Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies."
    - Linus Torvalds

  16. #16
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: [RESOLVED] Showing copying progress in a Label

    Give that man a rating, full of useful info !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width