Welcome to Dream.In.Code
Getting C# Help is Easy!

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!




Progress bar for my C# browser

 
Reply to this topicStart new topic

Progress bar for my C# browser, I have source code, but it's not working

aleisterbukowski
22 Apr, 2008 - 01:50 AM
Post #1

New D.I.C Head
*

Joined: 21 Apr, 2008
Posts: 2

CODE

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]
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Progress Bar For My C# Browser
22 Apr, 2008 - 04:54 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
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;
}
}


Hope this helps.

User is offlineProfile CardPM
+Quote Post

aleisterbukowski
RE: Progress Bar For My C# Browser
22 Apr, 2008 - 03:46 PM
Post #3

New D.I.C Head
*

Joined: 21 Apr, 2008
Posts: 2

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
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Progress Bar For My C# Browser
22 Apr, 2008 - 04:26 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,919



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Try the following to get the progress bar to work in the manner you are expecting.

CODE

private void WebBrowser1_ProgressChanged(object sender, System.Windows.Forms.WebBrowserProgressChangedEventArgs e)
{
        ProgressBar1.Maximum = Convert.ToInt32(e.MaximumProgress);
        ProgressBar1.Value = Convert.ToInt32(e.CurrentProgress);
}



User is offlineProfile CardPM
+Quote Post

baavgai
RE: Progress Bar For My C# Browser
22 Apr, 2008 - 05:24 PM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(aleisterbukowski @ 22 Apr, 2008 - 07:46 PM) *

so I just wrapped that with (int)

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...

QUOTE(jayman9 @ 22 Apr, 2008 - 08:26 PM) *

CODE

ProgressBar1.Maximum = Convert.ToInt32(e.MaximumProgress);
ProgressBar1.Value = Convert.ToInt32(e.CurrentProgress);



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. tongue.gif

User is offlineProfile CardPM
+Quote Post

baavgai
RE: Progress Bar For My C# Browser
22 Apr, 2008 - 06:05 PM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Alright, just gave this a spin.

csharp

private void Form1_Load(object sender, EventArgs e) {
this.webBrowser1.Navigate("http://www.dreamincode.net/");
}

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;
}

}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
Debug.WriteLine("webBrowser1_DocumentCompleted: " + e.Url);
}


Here are the trace results:
CODE

webBrowser1_ProgressChanged: 100, 10000
webBrowser1_ProgressChanged: 100, 10000
webBrowser1_ProgressChanged: 150, 10000
webBrowser1_ProgressChanged: 200, 10000
webBrowser1_ProgressChanged: 88800, 1000000
webBrowser1_ProgressChanged: 88800, 1000000
webBrowser1_ProgressChanged: 265300, 1000000
webBrowser1_ProgressChanged: 265300, 10000
webBrowser1_ProgressChanged: 265300, 10000
webBrowser1_ProgressChanged: 26817000, 1000000
webBrowser1_ProgressChanged: 26817000, 10000
webBrowser1_ProgressChanged: 27247800, 10000
webBrowser1_ProgressChanged: 27817000, 10000
webBrowser1_ProgressChanged: 27817000, 10000
webBrowser1_ProgressChanged: 33406530, 1000000
webBrowser1_ProgressChanged: 32944730, 1000000
webBrowser1_ProgressChanged: 1000000, 1000000
webBrowser1_DocumentCompleted: http://www.dreamincode.net/ads/adframe.php?n=a965628166bc&what=zone:23&resize=1
webBrowser1_DocumentCompleted: http://www.dreamincode.net/ads/adframe.php?n=a489449152bc&what=zone:6&resize=1
webBrowser1_ProgressChanged: -1, 1000000
webBrowser1_DocumentCompleted: http://a.lakequincy.com/f.ashx?channel=1&format=7&pageid=D0AFA85C-7C6F-304A-F352-724121D821E1&publisher=112&ypos=0&zone=1&country=US&placement=2007&creative=1234&
webBrowser1_DocumentCompleted: http://ads.aspalliance.com/displayad.aspx?t=12&m=115&guid=[guid]
webBrowser1_ProgressChanged: 0, 0
webBrowser1_ProgressChanged: 0, 0
webBrowser1_DocumentCompleted: http://www.dreamincode.net/


Looks like crap, huh? tongue.gif

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.

Good luck.

User is offlineProfile CardPM
+Quote Post

Jayman
RE: Progress Bar For My C# Browser
22 Apr, 2008 - 06:15 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,919



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
You have to get MaximumProgress each time the progress changes. For the very reasons you just stated.

As for browsers faking, that is more than likely true. They more than likely take some kind of average based on latency. Just my opinion.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:07PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month