1

我已经创建了一个多项选择测验,但我正在努力想出正确的代码来制作它,以便如果玩家选择包含正确答案的按钮,它的背景色会变成绿色几秒钟,然后下一个问题会自动出现并返回其正常的背景色。

此外,如果玩家选择了包含错误答案的按钮,则其背景颜色会变为红色,以向玩家表明他们的答案不正确。

为此,我有一个labelcalled lblQuestion,其中包含被问的问题,然后是四个buttons( btnAnswerA、和) btnAnswerB,它们都包含对该问题的潜在答案,当然,只有一个是正确的。btnAnswerCbtnAnswerD

我一直在摆弄代码一段时间,试图让它工作,但它并没有按照我的意愿工作,所以想来这里寻求一些关于这个问题的指导。

以下是与此问题相关的一些代码。我相信通过看它,你们中的许多人都会知道我哪里错了!

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)
    End If

    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)
    End If

    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)
    End If

    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)
    End If

    Call GetQuestion(questionNumber)
    prgbarOne.Value = 0

End Sub

基本上,我只需要一些帮助来了解如何让 BackColor 变成绿色/红色(取决于它们是对还是错),然后再展示下一个问题在哪里Call GetQuestion(questionNumber)以及知道如何获得原始 BackColor(这是灰色)在玩家点击按钮之前返回下一个问题。

希望这是有道理的,任何帮助都非常感谢。抱歉,我对编程还是很陌生(你可能会说)。另外,如果需要更多代码,我会提交。

4

3 回答 3

1

正如APrough指出的那样,您可以在每个Call GetQuestion(questionNumber).

您可以调整millisecondsTimeout表示线程被阻塞的毫秒数的参数。例如睡眠 2 秒:

System.Threading.Thread.CurrentThread.Sleep(2000)

但,

我确实尝试过,但是发生的情况是它冻结而不将按钮颜色更改为绿色/红色,而问题显示在标签中,因此最终延迟到显示新答案的下一个问题。

该行为的原因在 MSDN 上关于Application.DoEvents方法进行了解释,这也是解决方案

如果您在代码中调用 DoEvents,您的应用程序可以处理其他事件。例如,如果您有一个将数据添加到 ListBox 并将 DoEvents 添加到您的代码的表单,则当另一个窗口被拖到它上面时,您的表单会重新绘制。如果从代码中删除 DoEvents,则在按钮的单击事件处理程序完成执行之前,您的表单不会重新绘制。

这就是你发生的事情,背景色没有刷新,因为我猜你在GetQuestion方法中重置了背景色。所以你需要的是在方法Application.DoEvents之前调用这个方法()Thread.Sleep

您的代码将是:

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
于 2013-08-22T23:05:30.330 回答
0

谷歌搜索 vb.net 计时器控制。在他们单击按钮后启动计时器并处理它的 .Tick() 事件以重置按钮的 BG 颜色并开始下一个问题。这样,它将确保应用程序不会变得无响应并且会正确转换。

于 2013-08-22T12:50:50.513 回答
0

投放

System.Threading.Thread.CurrentThread.Sleep(1000)

就在您每次调用 GetQuestion(questionNumber) 之前。上面的 1000 是毫秒,所以根据需要调整。

于 2013-08-22T12:37:26.997 回答