0

I'm making a simple progress bar to be used as a splash screen within my application but when the code is executed, the loading bar does not reach the end of the progress bar.

I've used the code:

splashprogressbar.Increment(1) 
    If splashprogressbar.Value = 100 Then 
        Main_Menu.Show() 
        Me.Hide() 
    End If

to open a form when the progress bar reaches 100, which has been set as the maximum value.

The issue is more so related to appearance rather than functionality but i would still like to understand why this occurs and hopefully get a fix.

To clarify, the form Main_Menu, opens when the bar is about 3/4 of the way completed and i can't get my head around why this occurs. Any ideas?

Bustin Jieber
  • 111
  • 1
  • 3
  • 13
  • No matter what i do, the bar does not reach the end unless i change the maximum value to a number less than the conditioned value, in this case 100, but this would not execute the statement – Bustin Jieber Jul 11 '15 at 05:22
  • 1
    The ProgressBar actually "animates" to the new value (it doesn't jump immediately) and simply doesn't have time to get there because you called Hide(). See David Heffernan's [hack here](http://stackoverflow.com/a/5332770/2330053) in this question. – Idle_Mind Jul 11 '15 at 07:38
  • 1
    This is a well known "bug". There's a fix in the following dupe link: [Disabling .NET progressbar animation when changing value?](http://stackoverflow.com/questions/5332616/disabling-net-progressbar-animation-when-changing-value) – Bjørn-Roger Kringsjå Jul 11 '15 at 08:54

3 Answers3

-1

I couldn't imagine that VB has such a bug. Probably you are running an old or incompatible ".Net Framework" that causes that. You can try to set your Progressbars maximum programatically while running the program. Use this code in the Form1_Load event:

ProgressBar1.Maximum = 100

It should work. But if it doesn't, maybe it's just code related. Try to fill your progressbar this way (It must be in a timer):

If splashprogressbar.Value = splashprogressbar.Maximum Then 
    Main_Menu.Show() 
    Me.Hide() 
Else
    splashprogressbar.Value += 100
End If

Just a friendly advice: It seems you're simulating a fake loading bar which fills and then, only then, open the app (Potentially wasting the users time). Don't do that.

Fusseldieb
  • 1,324
  • 2
  • 19
  • 44
  • Oh of course, if this program is going to be used professionally then of course that would be silly to implement it the way that i have, it's just there for visual appeal, it is part of a module - Unfortunately the code you used doesn't seem to fix it either but thanks for trying – Bustin Jieber Jul 11 '15 at 06:35
-1

You could also try taking a look at the following link. This may help with your approach;

Trigger Background Worker

The below was copied from the link above...

Dim WithEvents bgWorker As New BackgroundWorker With { _
.WorkerReportsProgress = True, _
.WorkerSupportsCancellation = True}

Private Sub bgWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgWorker.DoWork
    For i As Integer = 0 To 100
        'Threw in the thread.sleep to illustrate what's going on.  Otherwise, it happens too fast.
        Threading.Thread.Sleep(250)
        bgWorker.ReportProgress(i)
    Next
End Sub

Private Sub bgWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgWorker.ProgressChanged
    If e.ProgressPercentage Mod 10 = 0 Then
        MsgBox(e.ProgressPercentage.ToString)
    End If
End Sub

Private Sub bgWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgWorker.RunWorkerCompleted
    MsgBox("Done")
End Sub
Community
  • 1
  • 1
-2

you could try something like this;

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

    Me.Show()

    Dim i As Integer

    splashprogressbar.Minimum = 0
    splashprogressbar.Maximum = 100

    If splashprogressbar.Value < splashprogressbar.Maximum Then

        For i = 0 To 100
            splashprogressbar.Value = i
            Application.DoEvents()
            System.Threading.Thread.Sleep(100)
        Next
    End If
    Me.Hide()
    MsgBox("Here I am") 'Use your "Main_Menu.Show" here

End Sub
  • This will freeze the main UI. – Visual Vincent Jul 11 '15 at 09:26
  • How so? You need to explain that a bit better... Works just fine here Vincent. You have to remember to adjust the thread/sleep time. I set that thread/sleep time to 100 as an exaggerated example. – Virtual Storm Jul 11 '15 at 13:07
  • Okay I see now I missed the `Application.DoEvents()` line, must've been because of the mobile version of the website. But keep in mind, even though this is supposed to be a splashscreen, using your code in a normal (closable) form will keep the application alive when you close it, until the progressbar reaches maximum. When reached maximum it will also throw a NullReferenceException exception, probably because the progressbar is disposed. – Visual Vincent Jul 11 '15 at 14:55
  • The user (Bustin Jeiber) who was asking the question, never mentioned about closing the application. I'm really not sure why you are seeing the NullReferenceException status either, but again the code was written to at least satisfy some idea to his question and demonstrate usability. Outside of that, there was no mention whether to keep the form alive or not, so the code was designed/written to what the question was and his following explanation... If there was a question on closing the form and having logic to fully close the form, there is plenty of logic that can be added to handle that. – Virtual Storm Jul 11 '15 at 17:27