Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new in c# programming and I have a hard time to understand on how to put a progress bar on my project file. Could you help me?

Here is my code:
C#
try
            {
                lblUpdate.Visible = true;
                lblUpdate.Refresh();

                string[] filenames = Directory.GetFiles(sTargetFolderPath);

                // Zip up the files - From SharpZipLib Demo Code
                using (ZipOutputStream s = new ZipOutputStream(File.Create(lblSaveTo.Text + "\\" + sZipFileName + ".pld")))
                {
                    s.SetLevel(9); // 0-9, 9 being the highest level of compression

                    byte[] buffer = new byte[4096];

                    foreach (string file in filenames)
                    {

                        ZipEntry entry = new ZipEntry(Path.GetFileName(file));

                        entry.DateTime = DateTime.Now;
                        s.PutNextEntry(entry);

                        using (FileStream fs = File.OpenRead(file))
                        {
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                s.Write(buffer, 0, sourceBytes);

                            } while (sourceBytes > 0);
                        }
                    }
                    s.Finish();
                    s.Close();
                }

                // remove the progress bar
                lblUpdate.Visible = false;

                // clean up files by deleting the temp folder and its content
                System.IO.Directory.Delete(lblSaveTo.Text + "\\TempZipFile\\", true);

                // Notify user
                MessageBox.Show("Compress file " + lblSaveTo.Text + " created.");

                // empty everything
                lstFilePaths.Items.Clear();
                lblSaveTo.Text = string.Empty;
                txtAddFile.Text = string.Empty;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Compression Operation Error");
            }
Posted
Comments
[no name] 31-Aug-12 16:58pm    
"// remove the progress bar
lblUpdate.Visible = false;"
You named your progress bar lblUpdate? Okay so what is it doing that you do not understand? Or what is it not doing? Or did you have sort of other question?
Larynill 1-Sep-12 7:35am    
Yes, I used the "lblUpdate" as an temporary progress bar. I just want to convert it to a regular progress bar toolbar to update the file status

ProgressBar is fairly simple to use. You need to set Minimum, Maximum and Value property of the ProgressBar.
Then in the loop use PerformStep method of the progressbar.

As per your e.g,
Get the length of the array string "filenames" using filename.length
C#
string[] filenames = Directory.GetFiles(sTargetFolderPath);

and set it as maximum value of the progressbar

Set the Minimum value to 1

Within the loop, where the "foreach (string file in filenames)" block ends, just before the line use
C#
progressbar1.PerformStep();



Here is a link which will give you a clear idea,
http://msdn.microsoft.com/en-us/library/system.windows.forms.progressbar.maximum.aspx[^]

Hope this helps
cheers
 
Share this answer
 
v3
Comments
Larynill 1-Sep-12 7:45am    
I just did what you said but still the progress bar don't start performing the step
do it like this

progressbar1.value = 0
progressbar1.maxvalue = 100

for i as integer = 0 to 100

progressbar1.value = i
next



///// i will give progress bar the value as the loop runs
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900