[RESOLVED] Picture box does not display without a messagebox.show after it displays.
I have a program and I am trying to display 9 picture boxes after a wait of 0.5 seconds after each one is displayed.
The only way that the picture box displays is if I put a messagebox.show after each display.
I am enclosing the code from my project.
Re: Picture box does not display without a messagebox.show after it displays.
To aid in the thread discussion, this is the attached code so others need not download the attachment:
Code:
Private Sub GET_BUG(sender As Object, e As EventArgs)
BUG_BOX.Visible = True
MY_CASE_BUGS = 1
START_BUGS:
If MY_CASE_BUGS = 1 Then
BUG_BOX.Image = WindowsApplication1.My.Resources.BUG_LEFT_BLUE
ElseIf MY_CASE_BUGS = 2 Then
BUG_BOX.Image = WindowsApplication1.My.Resources.BUG_LEFT_GREEN
ElseIf MY_CASE_BUGS = 3 Then
BUG_BOX.Image = WindowsApplication1.My.Resources.BUG_LEFT_YELLOW
ElseIf MY_CASE_BUGS = 4 Then
BUG_BOX.Image = WindowsApplication1.My.Resources.BUG_LEFT_RED
ElseIf MY_CASE_BUGS = 5 Then
BUG_BOX.Image = WindowsApplication1.My.Resources.BUG_STRAIGHT_BLUE
ElseIf MY_CASE_BUGS = 6 Then
BUG_BOX.Image = WindowsApplication1.My.Resources.BUG_STRAIGHT
ElseIf MY_CASE_BUGS = 7 Then
BUG_BOX.Image = WindowsApplication1.My.Resources.BUG_STRAIGHT_GREEN
ElseIf MY_CASE_BUGS = 8 Then
BUG_BOX.Image = WindowsApplication1.My.Resources.BUG_CROSS_EYE_BLUE
ElseIf MY_CASE_BUGS = 9 Then
BUG_BOX.Image = WindowsApplication1.My.Resources.BUG_STRAIGHT_RED
ElseIf MY_CASE_BUGS > 9 Then
GoTo END_BUGS
End If
MY_CASE_BUGS = MY_CASE_BUGS + 1
Threading.Thread.Sleep(500) ' 500 milliseconds = 0.5 seconds
MessageBox.Show("OK")
GoTo START_BUGS
END_BUGS:
BUG_BOX.Visible = False
End Sub
My guess is that your Thread.Sleep command is preventing the PictureBox from being "refreshed" with the new image, but that is just a guess. It might work as is by adding something like
Code:
BUG_BOX.Invalidate
right before your Thread.Sleep line, and then remove the MessageBox.
That being said, that code is a bit problematic on many levels. I would recommend reading up on how to use a Timer. In this case that seems like it would be a better fit than what you have.
Re: Picture box does not display without a messagebox.show after it displays.
Thread.Sleep has some uses, but only for very specific things, and this isn't one of them. You can pretty much say that if you are thinking that you need thread.Sleep in the UI, then you are doing something wrong. There may be some obscure case where it is the right thing to do, but they are VERY rare. So, what .Paul. said is the best way to go, but here's what is happening, for the most part:
You have created a loop using GOTO statements.
The first time through the loop, the first bug image is put into the box. That causes the picturebox to invalidate, which means that a Paint message is posted to the message queue. The message queue only gets processed when the UI has time to get to it, so the invalidation of the picturebox does nothing on its own. The image is there, but the picturebox hasn't been able to redraw, so you can't see it.
Next, you put the thread to sleep for some time. You may have thought that this gives the UI a chance to redraw the screen, but it's the UI that you put to sleep, so it doesn't redraw the screen...or do anything else. It just sleeps.
Finally, you advance the counter and start the loop again, which puts the second image into the picturebox and repeats the cycle. At no time did you let the UI redraw the picturebox, so you never get to see what you put into the picturebox, because you kept the UI busy all the time, even if it was just busy sleeping.
Therefore, use a timer. On every timer tick, put the next image into the picturebox and that's all. Between timer ticks, the UI is not sleeping, so it is free to process messages, which means that it will be free to redraw the picturebox and you will see your image. You'd just want to set the timer interval to 500.
Re: Picture box does not display without a messagebox.show after it displays.
here is some working code all you need to do is start a new project and add a LABEL1 in the designer and then copy the code.
Code:
Public Class Form1
Dim occurance As Integer = 1
'Dim bug_a_boo() As String = {"",WindowsApplication1.My.Resources.BUG_LEFT_BLUE,WindowsApplication1.My.Resources.BUG_LEFT_green,WindowsApplication1.My.Resources.BUG_LEFT_yellow,
' WindowsApplication1.My.Resources.BUG_LEFT_red,WindowsApplication1.My.Resources.BUG_straight_blue,WindowsApplication1.My.Resources.BUG_straight,
' WindowsApplication1.My.Resources.BUG_straight_green, WindowsApplication1.My.Resources.BUG_CROSS_EYE_BLUE,WindowsApplication1.My.Resources.BUG_STRAIGHT_RED}
Dim bug_a_boo() As String = {"", "a", "b", "c", "d", "e", "f", "g", "h", "i"}
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = bug_a_boo(occurance)
occurance += 1
If occurance > 9 Then
Timer1.Stop()
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Interval = 500
Timer1.Start()
End Sub
End Class
as you transition to your code you will need to use the other dim statement.