Bustin Jieber
Bustin Jieber

Reputation: 111

Wait for 1 second before starting code again - VB.NET

I desperately need help with a game I am making. For a bit of context, i am making a memory game and i have the following piece of code that is being troublesome. I have a bunch of labels on the form, 16 to be exact, with 1 randomly generated symbol placed in each. Each symbol appears in the labels twice.

      ------------------------------Continued----------------------------------------
                'MsgBox("hello") 'used to check if the second inccorect press shows up - it does show but instantly changes colour
                '''''''''''''''''NEED SOME CODE THAT PAUSES IT HERE'''''''''''''''
                labels(0).ForeColor = Color.DarkRed
                sender.ForeColor = Color.DarkRed
            End If
            flips = 1
        End If
    End If
    tmrmemory.Enabled = True ' starts the timer after the user clicks the first label
End Sub

What's supposed to happen is that when the labels clicked don't match, it should show both the clicked labels for a short period before changing them both back to "DarkRed" which is the colour of the form's background.

I have tried using a timer but then i can't use sender.forecolor=color.darkred because it is not declared globally.

I have also tried using the command Threading.Thread.Sleep(500) but it still doesn't show the second incorrect click. I know that the code i have used works because when i use the message box, i can see both symbols and when the two clicks are correct, it stays.

Upvotes: 0

Views: 12905

Answers (2)

Pradeep Kumar
Pradeep Kumar

Reputation: 6969

Threading.Thread.Sleep(500) will actually pause your code for half a second. However during this time it won't do anything, not even refresh your controls. To get the effect you want, you need to call the YourControl.Refresh method before calling Threading.Thread.Sleep to force the control to redraw immediately.

On a side note, I would advise you not to call Threading.Thread.Sleep on UI thread. It will give a feeling of program hang. Instead do your work on a separate thread. You can either do all the work yourself right from creating a separate thread to destroying it, or use the BackgroundWorker control which has all the functionality built in.

Here is the link to an article I wrote a long time ago regarding BackgroundWorker that might be useful for you:

http://www.vbforums.com/showthread.php?680130-Correct-way-to-use-the-BackgroundWorker

Upvotes: 2

BobbyTables
BobbyTables

Reputation: 4685

Declare a variable outside the sub that stores what label should be flipped when the timer ends.

Label click sets storedLabel = sender Timer tick sets storedLabel.ForeColor = Color.DarkRed

Upvotes: 0

Related Questions