472,131 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,131 software developers and data experts.

progressbar in a project

lee123
556 512MB
i have been trying to add a progressbar to my project but don't know how and the last post i have posted one person answered it but never came back to help me with how?

so i am asking again because this is something alot of people probably would want to know how to do it
so i am placing a code down and hopefullly some one could post the steps on how this could and can be done. not just with my project but others too. so please have a heart and show us who don't know how, but want to learn to do it

here is the code i am wanting to have a progressbar in it to show things happening

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCopy_Click()
  2.     Dim fso As New Scripting.FileSystemObject
  3.     Dim fld As Folder
  4.     cmdCopy.Caption = "Copying Folder!"
  5.     Set fld = fso.GetFolder(txtSourceOfFolder)
  6.     fld.Copy (txtDestinationOfFile)
  7.     Label1.Caption = "Copying Folder Is Completed!"
  8.     Beep
  9.     txtSourceOfFolder.Text = ""
  10.     txtDestinationOfFile.Text = ""
  11.     cmdExit.Enabled = True
  12. End Sub
as you can see from this code i have the button caption change when i click on it to say "Copying Folder!" and a label to say when it's done. this is great and all but a progressbar would look so much nicer and professional.


lee123
Sep 11 '08 #1
40 4545
The Visual Basic developer environment should offer a progress bar control for you to use. You can get it from the toolbox panel; the same place you got the command button from.
Sep 12 '08 #2
lee123
556 512MB
what? that's not what I'm Talking about. i know where the progressbar is located read the question again...
Sep 12 '08 #3
lee123
556 512MB
i want to know how to put it in my code to show something is going on, not that i don't know where it is.

lee123
Sep 12 '08 #4
Since your first post wasn't properly typed, I misunderstood.
It may not have been what you meant, but it's pretty much what you typed.

I haven't attempted this before, so I don't know if it will work. If it doesn't, you can use the overall structure to work out your own method.

Create the folder first, so that you have an FSO for it. Enumerate the initial folder's file collection for the total number of files, using the Files property of the FSO. Apply that value to the progress bar's Maximum property. Then, do the copy operation. In a loop, enumerate the files in the same way with the new folder, to work out the current number of files that exist within the new folder, and increment the progress bar's Value property until the maximum value has been reached. Finally, display the "completed" message.
Sep 12 '08 #5
lee123
556 512MB
yea i guess your right after reading it again it kinda says that, well i guess I'm Sorry About that.tristanlbaily well i have a question lets say someone bring over some music you want to copy from a external hard drive the files may vary and then i wouldn't know what to set the max value to. thats why i made this program to copy my friends folder of music or songs

lee123
Sep 12 '08 #6
lee123
556 512MB
o yea i forgot to tell you i don't know ho to do loops, well it's not that i haven't seen one i just never used them before. when i look at one i get confused on how to express it. sounds good when you say it in your head but when you typ it out it never works. that's why i placed the code in the first post so someone could help me place it in the code to understand how it's done. and i just noticed i misspelled your name sorry about that too.

lee123
Sep 12 '08 #7
lee123
556 512MB
ok i have this, but this isn't working right some of the file are small but this is taking too long to finish.


Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdCopy_Click()
  2. PBTask.Max = 100
  3. PBTask.Min = 0
  4. Dim i
  5. Dim fso As New Scripting.FileSystemObject
  6. Dim fld As Folder
  7. For i = 1 To 100
  8.         cmdCopy.Caption = "Copying Files!"
  9.         Set fld = fso.GetFolder(txtSourceOfFolder)
  10.         fld.Copy (txtDestinationOfFile)
  11.         PBTask.Value = (100 * (i / 100))
  12.       Next i
  13.         cmdExit.Enabled = True
  14.         txtSourceOfFolder.Text = ""
  15.         txtDestinationOfFile.Text = ""
  16. End Sub
I did a search on progressbar and i found this code. added my stuff in it and this is what i have so why is it taking too long? I don't Understand how this is done.....I need help!!!!!!!

lee123
Sep 13 '08 #8
Dont' use the code you've found; it's BAD.
The copy method is repeated 99 times, so it will of course be slow.

I presume you are using Visual Basic .NET, 2005, or 2008?

I am currently constructing code for the aforementioned; if you are using something older (i.e. Visual Basic 6 or VBA), please reply promptly.
Sep 15 '08 #9
I've just found something that you may be interested in:

http://vbnet.mvps.org/index.html?code/callback/filebackupcallback.htm

I tried making a simpler version, but it was taking too long. The sample above should be your "cup of tea".
Sep 15 '08 #10
lee123
556 512MB
actually I'm using visual basic 6.0 enterprise edition.

lee123
Sep 15 '08 #11
Tell me one thing. What you realy want is:

1 - Show to the user of your app exactly how much was done, or

2 - Show to the user of your app that your program is doing something?

In the first case you will need to find the file size and use this value as the input for the value of progress bar. In my option to much work for nothing.

In the second case, you can change the Style property of the status bar from Blocks (default value) to Marquee. Doing this the status bar will keep moving always. You can use the property Visible = true to show the status bar just before the file.copy instruction and Visible = False just after the copy is finished.

I hope this help.

Rogerio
Sep 15 '08 #12
lee123
556 512MB
Rpicilli, does both of these use the timer control because when i did it without the timer it takes too long, and actually i want to see what is going on so the second one

lee123
Sep 15 '08 #13
Did you look at the link I provided?

The code in the article should work in VB6.
Sep 16 '08 #14
lee123
556 512MB
yes i did and i actually made the program to see it worked and it worked but alot of coding.

lee123
Sep 16 '08 #15
smartchap
236 100+
Dear lee123
I have already given clue to same problem in some forum (which I don't remember at present).
First of all about your program taking too long to complete:
You have unneccessarily used For..Next loop, in this loop u have used copying of folder which is being copied 100 times again & again and Progress bar is increasing 1 to 100 simply showing that how many times folders is copied.
Now I just give u an idea (of course I will try a program and will post it here):
First, u read the number of files in the folder to be copied (say it is SFolder) and create a Destination folder (may be DFolder). Set max value of PBar to number of files in SFolder. Now copy all the files (using a loop) one by one from SFolder to DFolder and increase the value of PBar to number of files copied.
Second, read total number of bytes in SFolder, set max value of PBar to it. Copy files one by one and increase value of PBar as number of bytes copied increases.
Hope it will give an idea and help u.
Sep 16 '08 #16
lee123
556 512MB
hello..smartchap i have waited for you to answer some questions in our last post under "Progressbar" after waiting for a while i moved to this post to see if someone else could help. at the end of your post in the last posting you said:
("If you have any other query please post the code to understand the problem exactly and give solution.") well i waited and no one..anyway let me see if i understand what your saying: and try to figure it out..but the looping is what i don't understand yet becaue i haven't really used it in any of my projects i have made the looping you see is from someone elses code i tried..I'm afraid to say i don't understand how it works..i have to work on this looping until i understand it completely..thanks for answering and coming back to my problem.

lee123
Sep 16 '08 #17
smartchap
236 100+
Actually I was a little bit busy these days and couldn't open Internet so can't check the subscribed posts. Please try my suggestions and if need further help ask me. As I said I will try same and will post the code, but after some days as I am really busy these days.
Sep 16 '08 #18
Hi

This is a functional application that copy files and/or directory. Try to understant the CODE.

There in no easy way to create programs. If you do not have time to read a lot, you'll not be a good programmer. Excuse me to tell you that but I'm a 52 years old programmer and I learn something new every day.

Besides the code you can see the form image at www.maxidoc.com.br/help.png

In this image you'll see all the names used in the components and the design of the screen.

The components that has no name, let them as the default names.

Create a new project. Open the code of the form1. Copy all the following code into the Form1 code pane. Put the components and rename following the image that you can see in the address above.

I hope you can do it. May be you have problem but if you realy are a programmer, don't give up.



[code]

Imports System.IO

Public Class Form1

Private Sub butCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butCopy.Click
'Show the progress bar
Me.ProgressBar1.Visible = True
'Verify type of copy (Dir or File)
If Me.butCopy.Text = "Copy Directory" Then
'Copy Directory
subCopyDir(Me.txtFileSource.Text, Me.txtDirDestination.Text)
Else
'Copy file
subCopyFil(Me.txtFileSource.Text, Me.txtDirDestination.Text & "\" & Me.TextBox1.Text)
End If
'Hide the progressbar
Me.ProgressBar1.Visible = False
MessageBox.Show("Copy End")
End Sub

Private Sub butBrowseSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butBrowseSource.Click
Dim bResult As DialogResult
If Me.chkFileOrDir.Checked Then
'Directory
bResult = Me.FolderBrowserDialog1.ShowDialog
Else
'File
bResult = Me.OpenFileDialog1.ShowDialog
End If

'If cancel button was cicked return to main form
If bResult = Windows.Forms.DialogResult.Cancel Then Exit Sub

If Me.chkFileOrDir.Checked Then
'Put the Directory name on the text box
Me.txtFileSource.Text = Me.FolderBrowserDialog1.SelectedPath.ToString()
Else
'Put the file name on the text box
Me.txtFileSource.Text = Me.OpenFileDialog1.FileName.ToString()
'Put the same file name that could be change if necessary
Me.TextBox1.Text = Me.txtFileSource.Text.Substring(CType(Me.txtFileSo urce.Text.LastIndexOf("\"), Integer) + 1)
End If
End Sub

Private Sub butBrowseDestination_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butBrowseDestination.Click
Dim bResult As DialogResult

If Me.chkFileOrDir.Checked Then
'Copy all directory
bResult = Me.FolderBrowserDialog1.ShowDialog
'If cancel button was cicked return to main form
If bResult = Windows.Forms.DialogResult.Cancel Then Exit Sub
'Put the directory name on the text box
Me.txtDirDestination.Text = Me.FolderBrowserDialog1.SelectedPath.ToString()
Else
'Copy just a file
bResult = Me.FolderBrowserDialog1.ShowDialog
'If cancel button was cicked return to main form
If bResult = Windows.Forms.DialogResult.Cancel Then Exit Sub
'Put the directory name on the text box
Me.txtDirDestination.Text = Me.FolderBrowserDialog1.SelectedPath.ToString()
End If
End Sub

Private Sub chkFileOrDir_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFileOrDir.CheckedChanged
If Me.chkFileOrDir.Checked Then
Me.chkFileOrDir.Text = "Copy Entire Directory"
'Hide the file name
Me.TextBox1.Visible = False
Me.lblNewFileName.Visible = False
Me.butCopy.Text = "Copy Directory"
Else
Me.chkFileOrDir.Text = "Copy Just One File"
'Show the file name
Me.TextBox1.Visible = True
Me.lblNewFileName.Text = True
Me.butCopy.Text = "Copy File"
End If
End Sub

Private Sub subCopyDir(ByVal sOri As String, ByVal sDes As String)
Dim sFile As String

'copy all the files
For Each sFile In Directory.GetFiles(sOri)
File.Copy(sFile, sDes & "\" & sFile.Substring(sFile.LastIndexOf("\") + 1))
Application.DoEvents()
Next

End Sub

Private Sub subCopyFil(ByVal sOri As String, ByVal sDes As String)
Try
File.Copy(sOri, sDes)
Application.DoEvents()
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'hide progressbar
Me.ProgressBar1.Visible = False
End Sub
End Class


/[code]
Sep 17 '08 #19
lee123, you can use Timer to make your life easier (you may already know this), with a pseudo-progressbar

1)Drag a Timer control to your form. Set the interval e.g. to 500 and enable = false

2) Modify your code

[PHP]Private Sub cmdCopy_Click()
Dim fso As New Scripting.FileSystemObject
Dim fld As Folder
cmdCopy.Caption = "Copying Folder!"
Set fld = fso.GetFolder(txtSourceOfFolder)
fld.Copy (txtDestinationOfFile)
Timer1.enabled = True
End Sub[/PHP]


[PHP]Private Sub Timer1_Timer()
ProgressBar1.Value = ProgressBar1.Value + Timer1.Interval / 10
If (ProgressBar1.Value = 100) Then
Label1.Caption = "Copying Folder Is Completed!"
Beep
txtSourceOfFolder.Text = ""
txtDestinationOfFile.Text = ""
cmdExit.Enabled = True
Timer1.Enabled = False
End If
End Sub[/PHP]
Sep 17 '08 #20
lee123
556 512MB
well thank you rpicilli for that i will give it a try and your right about reading if you have the time. but going to school at night and working through the day is hard work but thank you for your help through this.

and xinariscy, your code looks intersting and simple so i am going to give this a try as well.

lee123
Sep 18 '08 #21
smartchap
236 100+
Dear Xinariscy
In your program there is no use of Progress Bar as it Increments only after the folder has been copied, i.e. value of progress bar is not dependent on size of folder. Yes it is dependent on Timer interval which makes it slow or fast. So I think it will not solve the problem lee123 has.
Sep 18 '08 #22
lee123
556 512MB
hey smartchap your right when i tried this way the folder copied and then at the end the progressbar worked strange.

lee123
Sep 18 '08 #23
smartchap you are right! That's why I say it's only a pseudo-progress bar. It's only for the eyes. This is absolutely no professional!...
Sep 18 '08 #24
smartchap
236 100+
I have tried code provided by tristanlbailey and its working fine (after some modifications). After I write a simple program will post it.
Sep 19 '08 #25
smartchap
236 100+
First of all sorry for the delay. Please see my submission at following URL:

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=71156&lngWId=1

It has full featured copying of folder to another folder with almost no bugs. If u find any bug please let me know remove it.

Dear lee123 I have also answered for ur animation post (for progress bar). In a similar way u can solve that problem also.
Sep 30 '08 #26
smartchap
236 100+
Dear lee
I think u must have downloaded CopyFolder_PBar from planet source code (PSC). If it helped u please reply and vote the submission by scrolling the screen of the code )below Download Code) in PSC.
Oct 1 '08 #27
lee123
556 512MB
no i got the code from a book i have and it was different than what i have posted (made a little adjustments on it) i thought it would work but didn't.

lee123
Oct 4 '08 #28
smartchap
236 100+
Dear lee
U can download my code and it works fine. However, which code u have from book and is not working, please upload here with the problem u are facing so that I will try to resolve the problem.
Oct 6 '08 #29
lee123
556 512MB
hello, i have posted my project on the web so you can see what it looks like, it is different from yours and i have tried yours and it works great but there are things on mine that i don't know where to put your code if you want to see it go to this address or click

here:

the code i tried to use is on the "progressbar post"

Expand|Select|Wrap|Line Numbers
  1. For P = 1 to 100 
  2.        Progressbar1.value = P 
  3.               For x = 1 to 100                
  4.        Next x 
  5. Next P
how can i put this kinda code for the progressbar with the timer with my code on the web page

lee123

lee123
Oct 7 '08 #30
lee123
556 512MB
Oh Yea I will remove this from the web page in a few days because i have palns for this web page. i am building

lee123
Oct 7 '08 #31
lee123
556 512MB
hey smart chap i have deleted the picture i wanted to show you on the web page if you want to see it againg post so

lee123
Oct 13 '08 #32
smartchap
236 100+
Dear lee
Today I was checking my subscriptions and found ur post. Please tell me in detail what u want to be done on clicking each command button (Reset, Start Copying, Move Folder,etc.) and associated Text boxes. Because by selecting folder form directory list box you can put its full path in a textbox and by selecting another dir list box can select destination folder then by clicking COPY command button can copy it. Similarly can move a folder. So please tell details of controls being used by you so that help could be provided. However from my code u can make ur programme. Thanks.
Dec 22 '08 #33
EYE4U
75
Lee do you want a code which will ask you to copy files from any external device either its a Harddrive, CD/DVD or a PenDrive and copy all music files or whatever to your harddrive???

Regards
ARUZ
Mar 29 '09 #34
lee123
556 512MB
now that would be great if you can show me how that's done.

lee123
Mar 30 '09 #35
EYE4U
75
I was a fool i didnt see the date of thread and just answered it :P
Sorry admins
Mar 30 '09 #36
lee123
556 512MB
does this mean your not going to show me how now because of the date of the thread? because i still want to know.


lee123
Mar 30 '09 #37
EYE4U
75
NONO
I ll upload the example...
Mar 30 '09 #38
EYE4U
75
This example will automatically detect the drive/device attached or inserted in your computer and will count the number of files in it.

Regards
ARUZ
Attached Files
File Type: zip App.zip (2.4 KB, 128 views)
Apr 3 '09 #39
lee123
556 512MB
@EYE4U

In your last post it doesn't show me that, it just shows how many files in a drive, but i like that? how do you copy the music & how can i use a progressbar in it. because really that's what i want to learn how to do it (the progressbar), but don't know how or understand how to and when to?

lee123
Apr 5 '09 #40
EYE4U
75
Sorry i posted the wrong attachment....
Apr 5 '09 #41

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by jomargon | last post: by
2 posts views Thread by jez123456 | last post: by
8 posts views Thread by needin4mation | last post: by
4 posts views Thread by pmcguire | last post: by
3 posts views Thread by Mitchell Vincent | last post: by
2 posts views Thread by =?Utf-8?B?QWFyb24=?= | last post: by
lee123
7 posts views Thread by lee123 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.