Search code examples
c#browserprogress

webBrowser_ProgressChanged giving negative output for progressBar


I'm trying to complete a simple progressbar to my web browser control, but can't understand why I'm getting the following error:

"Value of '-1' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'. Parameter name: Value"

I'm not sure how the progress is returning a -1.

Here's the code I currently have tried:

    private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
    {
        int max = (int)e.MaximumProgress;
        int current = (int)e.CurrentProgress;
        int min = 0;

        progressBar1.Minimum = 0;
        progressBar1.Maximum = max;
        progressBar1.Value = current;
    }

Is anyone able to see where I might be going wrong?


Solution

  • If you read the documentation about what WebBrowserProgressChangedEventArgs.CurrentProgress will contain, it says that it's

    "The number of bytes that have been loaded or -1 to indicate that the download has completed."

    So, it'd be logical to assume that when you get a value of -1, your progressbar should be completed - or hidden, however you want to handle it.