As APrough has pointed out, you can use the Thread.Sleep Method right before each of your Call GetQuestion(questionNumber)
.
You can adjust the millisecondsTimeout
parameter that represents the number of milliseconds for which the thread is blocked. For example to sleep for 2 seconds:
System.Threading.Thread.CurrentThread.Sleep(2000)
But,
I did try that, but what happens is that it freezes without changing the button colour to green/red whilst the question is shown in the label, so ends up delaying onto the next question where new answers are shown.
The reason of that behavior is explained on MSDN about the Application.DoEvents method, that is also the solution:
If you call DoEvents in your code, your application can handle the other events. For example, if you have a form that adds data to a ListBox and add DoEvents to your code, your form repaints when another window is dragged over it. If you remove DoEvents from your code, your form will not repaint until the click event handler of the button is finished executing.
It's what happen for you, the backcolor is not refreshed because I guess you reset the backcolor in the GetQuestion
method. So what you need is to call this method (Application.DoEvents
) before the Thread.Sleep
method.
Your code would be:
Private Sub btnAnswerA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnswerA.Click
If strAnswer = "A" Then
btnAnswerA.BackColor = Color.Green
intScore = intScore + 100
lblScore.Text = intScore
Else
btnAnswerA.BackColor = Color.Red
'Call GetQuestion(questionNumber) no need here, it will be called after
End If
'Make sure backcolor is refreshed
Application.DoEvents()
'Wait for two second
System.Threading.Thread.Sleep(2000)
Call GetQuestion(questionNumber)
prgbarOne.Value = 0
End Sub
Private Sub btnAnswerB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnswerB.Click
If strAnswer = "B" Then
btnAnswerB.BackColor = Color.Green
intScore = intScore + 100
lblScore.Text = intScore
Else
btnAnswerB.BackColor = Color.Red
'Call GetQuestion(questionNumber) no need here, it will be called after
End If
'Make sure backcolor is refreshed
Application.DoEvents()
'Wait for two second
System.Threading.Thread.Sleep(2000)
Call GetQuestion(questionNumber)
prgbarOne.Value = 0
End Sub
Private Sub btnAnswerC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnswerC.Click
If strAnswer = "C" Then
btnAnswerC.BackColor = Color.Green
intScore = intScore + 100
lblScore.Text = intScore
Else
btnAnswerC.BackColor = Color.Red
'Call GetQuestion(questionNumber) no need here, it will be called after
End If
'Make sure backcolor is refreshed
Application.DoEvents()
'Wait for two second
System.Threading.Thread.Sleep(2000)
Call GetQuestion(questionNumber)
prgbarOne.Value = 0
End Sub
Private Sub btnAnswerD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAnswerD.Click
If strAnswer = "D" Then
btnAnswerD.BackColor = Color.Green
intScore = intScore + 100
lblScore.Text = intScore
Else
btnAnswerD.BackColor = Color.Red
'Call GetQuestion(questionNumber) no need here, it will be called after
End If
'Make sure backcolor is refreshed
Application.DoEvents()
'Wait for two second
System.Threading.Thread.Sleep(2000)
Call GetQuestion(questionNumber)
prgbarOne.Value = 0
End Sub