how to show progress bar while save data into files in c#?

Farshad Valizade 501 Reputation points
2024-05-31T05:20:47.42+00:00

I have a winform app. in this form I put a button.

this button open a file dialog user select multi files and then it convert all those files.I want to show progress bar after the user select the files.show percent and after all files finished then it stop the progressbar and make it 100%.

this is my btn convert code:

        private void btnConvert_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.Filter = "Dwg Files(*.dwg)|*.dwg";
                if (openFileDialog1.ShowDialog(this) != DialogResult.OK) return;
                if (openFileDialog1.FileName != null)
                {
                    foreach (var file in openFileDialog1.FileNames)
                    {
                        CadDocument doc = DwgReader.Read(file);
                        doc.Header.Version = ACadVersion.AC1032;
                        string dxfPath = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file) + ".dxf");
                        using (DxfWriter writer = new DxfWriter(dxfPath, doc, false))
                        {
                            writer.OnNotification += onNotification;
                            writer.Write();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Developer technologies C#
{count} votes

Accepted answer
  1. Anonymous
    2024-05-31T08:01:37.2+00:00

    Hi @Farshad Valizade , Welcome to Microsoft Q&A,

    1. Make sure the file dialog allows multiple file selections (openFileDialog1.Multiselect = true).
    2. Get the list of files selected by the user and set the progress bar's maximum value to the number of files (progressBar1.Maximum = files.Length).
    3. After each file is converted, increase the progress bar's value (progressBar1.Value++).
    4. After all files have been processed, set the progress bar's value to the maximum value and display a completion message.
    using System;
    using System.IO;
    using System.Windows.Forms;
    
    namespace YourNamespace {
        public partial class YourForm: Form {
            public YourForm() {
                InitializeComponent();
            }
    
            private void btnConvert_Click(object sender, EventArgs e) {
                try {
                    openFileDialog1.Filter = "Dwg Files(*.dwg)|*.dwg";
                    openFileDialog1.Multiselect = true; // Ensure multiple selections are allowed
                    if (openFileDialog1.ShowDialog(this) != DialogResult.OK) return;
    
                    string[] files = openFileDialog1.FileNames;
                    if (files.Length == 0) return;
    
                    progressBar1.Minimum = 0;
                    progressBar1.Maximum = files.Length;
                    progressBar1.Value = 0;
                    progressBar1.Visible = true;
    
                    foreach(var file in files) {
                        CadDocument doc = DwgReader.Read(file);
                        doc.Header.Version = ACadVersion.AC1032;
                        string dxfPath = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file) + ".dxf");
                        using(DxfWriter writer = new DxfWriter(dxfPath, doc, false)) {
                            writer.OnNotification += onNotification;
                            writer.Write();
                        }
    
                        // Update progress bar
                        progressBar1.Value++;
                    }
    
                    progressBar1.Value = progressBar1.Maximum;
                    MessageBox.Show("All files converted! ");
                } catch (Exception ex) {
                    MessageBox.Show(ex.Message);
                }
            }
    
            private void onNotification(object sender, NotificationEventArgs e) {
                // Handle notification events
            }
        }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anushka 320 Reputation points
    2024-05-31T06:26:03.41+00:00

    Hello friend, I hope you're having a nice day,

    To show a progress bar while converting multiple files in your WinForms application, you can follow these steps:

    1. Add a ProgressBar control to your form.
    2. Set its Minimum and Maximum properties according to your progress range (typically 0 to 100).
    3. Update the progress bar value inside your file conversion loop.
    4. Handle the progress bar's visibility and value accordingly.

    Here's an updated version of your btnConvert_Click method with the progress bar implementation:

    private void btnConvert_Click(object sender, EventArgs e)
    {
        try
        {
            openFileDialog1.Filter = "Dwg Files(*.dwg)|*.dwg";
            if (openFileDialog1.ShowDialog(this) != DialogResult.OK) return;
            
            progressBar1.Minimum = 0;
            progressBar1.Maximum = openFileDialog1.FileNames.Length;
            progressBar1.Value = 0;
            progressBar1.Visible = true;
            int progress = 0;
            foreach (var file in openFileDialog1.FileNames)
            {
                CadDocument doc = DwgReader.Read(file);
                doc.Header.Version = ACadVersion.AC1032;
                string dxfPath = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file) + ".dxf");
                
                using (DxfWriter writer = new DxfWriter(dxfPath, doc, false))
                {
                    writer.OnNotification += onNotification;
                    writer.Write();
                }
                // Update progress bar
                progress++;
                progressBar1.Value = (int)((progress / (double)openFileDialog1.FileNames.Length) * 100);
            }
            // All files converted, hide progress bar and set value to 100%
            progressBar1.Visible = false;
            progressBar1.Value = 100;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    
    

    Make sure you have a ProgressBar named progressBar1 added to your form. This code will display the progress of file conversion as a percentage. Adjust the progress bar's appearance and behavior as needed for your application. I hope this helps. Have a nice day!!!

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.