Join 136,085 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,547 people online right now. Registration is fast and FREE... Join Now!
private void WebBrowser1_ProgressChanged(object sender, System.Windows.Forms.WebBrowserProgressChangedEventArgs e) { int d; int t; ProgressBar1.Visible = true; d = (int)e.CurrentProgress; t = (int)e.MaximumProgress; if ((d <= 0)) { ProgressBar1.Value = 0; ProgressBar1.Visible = false; } else { ProgressBar1.Value = Math.Min(ProgressBar1.Maximum, Convert.ToInt32(Math.Floor((ProgressBar1.Maximum * (d / t))))); } }
Error 1 The call is ambiguous between the following methods or properties: 'System.Math.Floor(decimal)' and 'System.Math.Floor(double)' C:\Documents and Settings\carl\My Documents\Visual Studio 2008\Projects\SimpleBrowser\SimpleBrowser\Form1.cs 96 85 SimpleBrowser [/source]
You're getting this because inside your "Math.Floor" you're doing this "long * (int / int)". The result is of type long. It doesn't know how to floor a long. Also, because d and t are int, you'll never get the float you're looking for.
Perhaps something like this:
csharp
private void WebBrowser1_ProgressChanged(object sender, System.Windows.Forms.WebBrowserProgressChangedEventArgs e) { // if it's done or not going, we don't want it visible ProgressBar1.Visible = (!(e.CurrentProgress<=0 || e.CurrentProgress==e.MaximumProgress)); if (ProgressBar1.Visible) { // this gets the percentage and multiplies it by integer 100 // giving an int result between 0 and 100 ProgressBar1.Value = ((float)e.CurrentProgress/(float)e.MaximumProgress) * 100; } else { // this is probably complete redundant, since we don't see it ProgressBar1.Value = 0; } }
Error 1 Cannot implicitly convert type 'float' to 'int'. An explicit conversion exists (are you missing a cast?) C:\Documents and Settings\carl\My Documents\Visual Studio 2008\Projects\SimpleBrowser\SimpleBrowser\Form1.cs 90 38 SimpleBrowser
so I just wrapped that with (int)
and it compiled just fine, but the progressbar didn't work at all. I tried myspace.com figuring this time of an hour with the traffic I'd have to get some type of loading problem.
This post has been edited by aleisterbukowski: 22 Apr, 2008 - 03:49 PM
and it compiled just fine, but the progressbar didn't work at all.
Odd. The "* 100" should have forced an implicit int down step of the result. I note you're using VS2008, that may behave differently than I expected. If you're just trying to pull a web page, maybe I'll throw something together and give it a spin...
I considered this approach, but didn't like the idea of reinitializing the ProgressBar1.Maximum for every tick. I'd probably initialize it once using a flag, but didn't want to add extra complexity. Still, if it works over the one I offered that didn't, it's the better choice.
private void webBrowser1_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e) { // if it's done or not going, we don't want it visible Debug.WriteLine("webBrowser1_ProgressChanged: " + e.CurrentProgress + ", " + e.MaximumProgress); this.progressBar1.Visible = (!(e.CurrentProgress <= 0 || e.CurrentProgress == e.MaximumProgress)); if (this.progressBar1.Visible) { // this gets the percentage and multiplies it by integer 100 // giving an int result between 0 and 100 this.progressBar1.Value = (int)(((float)e.CurrentProgress / (float)e.MaximumProgress) * 100); } else { // this is probably complete redundant, since we don't see it this.progressBar1.Value = 0; }
A web page is not just one http call, but an initial fetch and then a get for every the other external element referenced by the document that the browser parses. At to that the non linear nature of events...
In the end, progress bars are all faking. The almost always are. They make a guess and give the user a warm fuzzy. There's probably a clever way to do this with a percentage increment hit each time the ProgressChanged event fires.