Using Progressbar in C # windows application

The user will be able to find some document on the local computer, and I want to show the user a progress bar while searching in my program. To be more clear, I have a foreach loop for which I want to bind my progress bar to show the progress. my foreach loop and progress should be running at the same time. Can this be done?

+2


a source to share


1 answer


From ProgressBar Class

You can try something like

private void CopyWithProgress(string[] filenames)
{
    // Display the ProgressBar control.
    pBar1.Visible = true;
    // Set Minimum to 1 to represent the first file being copied.
    pBar1.Minimum = 1;
    // Set Maximum to the total number of files to copy.
    pBar1.Maximum = filenames.Length;
    // Set the initial value of the ProgressBar.
    pBar1.Value = 1;
    // Set the Step property to a value of 1 to represent each file being copied.
    pBar1.Step = 1;

    // Loop through all files to copy.
    for (int x = 1; x <= filenames.Length; x++)
    {
        // Copy the file and increment the ProgressBar if successful.
        if(CopyFile(filenames[x-1]) == true)
        {
            // Perform the increment on the ProgressBar.
            pBar1.PerformStep();
        }
    }
}

      



EDIT

For a longer task, you can also use the BackgroundWorker Class

+4


a source







All Articles