locked
Progress Bar Problems RRS feed

  • Question

  • I'm writing a web browser in Visual Basic 2010 Express and I would like to add a progress bar, it all appears to work when I run the program and navigate to a website, but usually (about halfway through) it reaches an error where the progress is either above the maximum or below the minimum.

    Here's the code

    Private Sub WebBrowser1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged
            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = e.MaximumProgress
            ProgressBar1.Value = e.CurrentProgress
            Dim per As Integer
            per = e.CurrentProgress / e.MaximumProgress
            If ProgressBar1.Value = e.MaximumProgress Then
                Label1.Text = "Done"
            Else : Label1.Text = "Loading " & per & "%"
            End If
        End Sub
    Please help me

    Thursday, September 15, 2011 3:59 PM

Answers

  • Private Sub WebBrowser1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged
        Dim CurrentProgress As Double = e.CurrentProgress
        Dim MaximumProgress As Double = e.MaximumProgress
        ProgressBar1.Maximum = CInt(e.MaximumProgress)
        If CurrentProgress = -1 = False Then
            ProgressBar1.Value = CInt(CurrentProgress)
            Dim LabelString As String = CStr(Math.Round(CurrentProgress / MaximumProgress, 2) * 100) & "% Complete"
            If LabelString = "NaN% Complete" = False Then
                Label1.Text = LabelString
            Else
                Label1.Text = "Download Complete"
            End If
        End If
    End Sub



    If you want something you've never had, you need to do something you've never done.
    • Edited by Paul Ishak Thursday, September 15, 2011 5:45 PM
    • Marked as answer by ski-fi dude 1996 Thursday, September 15, 2011 5:50 PM
    Thursday, September 15, 2011 5:39 PM

All replies

  • Some times when a page redirect or connect to a remote location the maximum progress changes.

    e.g

    when signing in to a Live mail account it goes through multiple sub-domains but the sign in page remains constant

     

    Private Sub WebBrowser1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged
            ProgressBar1.Minimum = 0
            ProgressBar1.Maximum = e.MaximumProgress 
            ProgressBar1.Value = e.CurrentProgress
            Dim per As Integer
    
     'evaluate both values to check if they are correct
    Dim cp as integer =  e.CurrentProgress 
    dim mp as integer = e.MaximumProgress
    ' write code to check if cp is less then mp or = to zero and mp is greater then cp
    
           per = e.CurrentProgress / e.MaximumProgress
            If ProgressBar1.Value = e.MaximumProgress Then
                Label1.Text = "Done"
            Else : Label1.Text = "Loading " & per & "%"
            End If
        End Sub
    


     


    Please mark those posts as answer which answers your question. Faraz
    • Edited by Faraz Zone Thursday, September 15, 2011 4:14 PM
    Thursday, September 15, 2011 4:13 PM
  • Thanks, this is helpful, I understand what's going wrong, but I'm practically a newbie at writing my own code (aargh why am i so stupid)

     


    It's pronounced Sky - Fi, and its gonna rule the world,cos i'm inventing it
    Thursday, September 15, 2011 4:38 PM
  • See if this helps?

     


    CODE
     

       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

     

           Label1.Text = ""

     

           With ProgressBar1

               .Minimum = 0

               .Maximum = 100

           End With

     

           WebBrowser1.Navigate("http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/586c1c6c-6055-4cfa-a55b-193bbec40eb9")

     

       End Sub

     

     

     

       Private Sub WebBrowser1_ProgressChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged

     

           Dim pctDone As Double = (e.CurrentProgress / e.MaximumProgress) * 100

     

           If pctDone > 0 Then

               With ProgressBar1

                   If pctDone > 100 Then

                       .Value = 100

                   Else

                       .Value = CInt(pctDone)

                   End If

                   .Visible = True

                   .Refresh()

               End With

     

               Label1.Text = "Loading (" & pctDone.ToString("f0") & "%)"

           Else

               ProgressBar1.Visible = False

               Label1.Text = ""

           End If

     

       End Sub


       

    Thursday, September 15, 2011 4:57 PM
  • I wrote in that code and got this


    It's pronounced Sky - Fi, and its gonna rule the world,cos i'm inventing it
    Thursday, September 15, 2011 5:18 PM
  • Ski,

    Did you remember to set them as shown in my code's form load event?

           With ProgressBar1

               .Minimum = 0

               .Maximum = 100

           End With

    Thursday, September 15, 2011 5:21 PM
  • Private Sub WebBrowser1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserProgressChangedEventArgs) Handles WebBrowser1.ProgressChanged
        Dim CurrentProgress As Double = e.CurrentProgress
        Dim MaximumProgress As Double = e.MaximumProgress
        ProgressBar1.Maximum = CInt(e.MaximumProgress)
        If CurrentProgress = -1 = False Then
            ProgressBar1.Value = CInt(CurrentProgress)
            Dim LabelString As String = CStr(Math.Round(CurrentProgress / MaximumProgress, 2) * 100) & "% Complete"
            If LabelString = "NaN% Complete" = False Then
                Label1.Text = LabelString
            Else
                Label1.Text = "Download Complete"
            End If
        End If
    End Sub



    If you want something you've never had, you need to do something you've never done.
    • Edited by Paul Ishak Thursday, September 15, 2011 5:45 PM
    • Marked as answer by ski-fi dude 1996 Thursday, September 15, 2011 5:50 PM
    Thursday, September 15, 2011 5:39 PM
  • Thanks for the help, that longer bit of code that you posted and deleted afterward fixed everything. Unfortunately i could not mark it as the answer 
    It's pronounced Sky - Fi, and its gonna rule the world,cos i'm inventing it
    Thursday, September 15, 2011 5:41 PM
  • Thanks for the help, that longer bit of code that you posted and deleted afterward fixed everything. Unfortunately i could not mark it as the answer 
    It's pronounced Sky - Fi, and its gonna rule the world,cos i'm inventing it

    The one above does the job too =), Mark that one as answer O:)
    If you want something you've never had, you need to do something you've never done.
    • Edited by Paul Ishak Thursday, September 15, 2011 5:46 PM
    Thursday, September 15, 2011 5:42 PM