0

I want my progress bar to start running when the user presses the Validate button. However I can only get the progress bar to move when I click on it instead. How do I link my progress bar to start when the I press the Validate button.

    private void ValidateButton_Click(object sender, EventArgs e)
    {           

        int counter = 0;
        string line;

        // Read the file and display it line by line.  
        System.IO.StreamReader file =
        new System.IO.StreamReader(FileNameBox.Text);
        while ((line = file.ReadLine()) != null)
        {
            System.Console.WriteLine(line);
            counter++;
        }

        file.Close();
        System.Console.WriteLine("There were {0} records.", counter);
        // Suspend the screen.  
        System.Console.ReadLine();
    }


    private void ProgressBar_Click()
    {
        // Display the ProgressBar control.
        ProgressBar.Visible = true;
        // Set Minimum to 1 to represent the first file being copied.
        ProgressBar.Minimum = 1;
        // Set Maximum to the total number of files to copy.
        ProgressBar.Maximum = FileNameBox.TextLength;
        // Set the initial value of the ProgressBar.
        ProgressBar.Value = 1;
        // Set the Step property to a value of 1 to represent each file being copied.
        ProgressBar.Step = 1;
        // Loop through all files to copy.
        for (int x = 1; x <= FileNameBox.TextLength; x++) ;

    }
}

The FileNameBox is a file path the user has chosen. This works as intended.

Matthiee
  • 431
  • 4
  • 14
  • You said that ProgressBar_Click does what you want to? Then why not just add a call to it inside the ValidateButton_Click method? – MindSwipe Mar 27 '19 at 12:32
  • What doesn't work exactly? The progress bar is *running* by **changing** its `Value` constantly. What you mean by *"progress bar to start"*? Do you want [indeterminate](https://stackoverflow.com/q/312936/1997232) one? – Sinatr Mar 27 '19 at 12:33
  • The progress bar should not be clickable. Instead, you should turn it into it's own method and call it in the ValidateButton click. You may also want to check out other examples of progress bars: https://stackoverflow.com/questions/1259949/how-do-i-implement-a-progress-bar-in-c – Bedir Mar 27 '19 at 12:46

1 Answers1

0

I guess you are using winform, and you want show the realtime progress.

I think you can initialize the progressBar on Validate Button click, and update ProgressBar.Value by a Timer.

Btw, I don't understand 'ProgressBar.Maximum = FileNameBox.TextLength;' meaning, TextLength means text length, the file path text length, doesn't it ?

hideDragon
  • 344
  • 2
  • 5