vb.net 如果按钮包含正确答案,如何获取按钮的背景颜色以更改颜色?Visual Basic 2010

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18380477/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:45:23  来源:igfitidea点击:

How can I get the backcolor of a button to change colour if it contains the correct answer? Visual Basic 2010

vb.netwinformsbackcolor

提问by KSR5

I have created a multiple choice quiz, but am struggling with coming up with the correct code to make it so that if the player selects the button containing the correct answer, its BackColor will turn green for a few seconds, before the next question appears automatically and returns to its normal BackColor.

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

Also, if the player selects the button containing the wrong answer, then its BackColor turns red to establish to the player that they were incorrect.

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

For this, I have a labelcalled lblQuestion, which contains the question being asked and then four buttons(btnAnswerA, btnAnswerB, btnAnswerCand btnAnswerD) which all contain a potential answer to the question, with of course, only one being correct.

对于这一点,我有一个label叫做lblQuestion,其中包含所提出的问题和然后是四个buttonsbtnAnswerAbtnAnswerBbtnAnswerCbtnAnswerD),它都含有一个潜在的问题的答案,当然是有,只有一个是正确的。

I have been fiddling around with the code for sometime, trying to get it to work, but it doesn't work how I want to, so thought to come on here for some guidance in this matter.

一段时间以来,我一直在摆弄代码,试图让它发挥作用,但它没有按照我想要的方式工作,所以想来这里寻求有关此事的一些指导。

Below is some of the code relating to this issue. I am sure by looking at it, many of you will know where I am going wrong!

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

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

Basically, I just need some help with knowing how to get the BackColor to turn green/red (depending on whether they're right or wrong) for a few moments before showing the next question where Call GetQuestion(questionNumber)is and knowing how to get the original BackColor (which is Gray) to return for the next question before the player has clicked on a button.

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

Hope this makes sense and any help is really appreciated. I apologise, I am still new to programming (as you may tell). Also, if more code is needed, I will submit it.

希望这是有道理的,任何帮助都非常感谢。我很抱歉,我对编程还是个新手(如您所见)。另外,如果需要更多代码,我会提交。

采纳答案by Chris

As APrough has pointed out, you can use the Thread.SleepMethod right before each of your Call GetQuestion(questionNumber).

作为APrough已经指出的那样,你可以使用Thread.sleep代码每个的前法权Call GetQuestion(questionNumber)

You can adjust the millisecondsTimeoutparameter that represents the number of milliseconds for which the thread is blocked. For example to sleep for 2 seconds:

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

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.DoEventsmethod, that is also the solution:

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

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.

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

It's what happen for you, the backcolor is not refreshed because I guess you reset the backcolor in the GetQuestionmethod. So what you need is to call this method (Application.DoEvents) before the Thread.Sleepmethod.

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

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

回答by APrough

Put in

投放

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

right before each of your Call GetQuestion(questionNumber). The 1000 above is milliseconds, so adjust as you need.

就在您每次调用 GetQuestion(questionNumber) 之前。上面的 1000 是毫秒,因此请根据需要进行调整。

回答by Nicholas Post

Do a Google search for vb.net timer control. Start a timer after they click the button and handle it's .Tick() event to reset the BG colors of the buttons as well as start the next question. This way it will ensure that the application does not become unresponsive and will properly transition.

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