根据正确/错误答案更改按钮的背景颜色 [英] Change back color of buttons based on correct/incorrect answer

查看:25
本文介绍了根据正确/错误答案更改按钮的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点想不通.

我希望按钮的背景颜色根据点击的按钮是正确答案还是错误答案而改变,即如果点击的按钮是正确答案,则背景颜色变为黄绿色,如果是错误答案,所有错误答案的背景颜色变为红色,正确答案的背景颜色为黄色绿色,以便用户可以查看他们是否答对了问题或是否答错,以及哪个答案是正确的一个.

I want the background color of the buttons to change based on whether the button clicked is the right answer or the wrong answer i.e. if the clicked button is the right answer, the background color changes to YellowGreen and if it is the wrong answer, the background color of all the wrong answers chages to red red and the right answer has a background color of YellowGreen so that the user is able to see if they got the question right or if they got it wrong, and which answer is the correct one.

这就像出现了第一个问题,有四个按钮,我点击正确答案,它变成绿色,错误答案变成红色,但是当我点击下一步并转到下一个问题时,它不再改变颜色.如果我点击一个不正确的答案,颜色也不会因为某种原因而改变,只有当我先点击正确的答案.

It's like the first question comes up, there's four buttons, I click on the correct answer, it goes green and the wrong answers go red but then when I click Next and go to the next question, it no longer changes colors. The colors also don't change for some reason if I click one of the incorrect answers, only if I click the correct one first.

这四个按钮分别命名为 button4、button5、button6 和 button7.我的示例代码(所有四个按钮都有这个):

The four buttons are named button4, button5, button6 and button7. Example code I have (Have this for all four buttons):

if (qCorrect == 1)
{
    button4.BackColor = Color.YellowGreen;
    button5.BackColor = Color.Red;
    button6.BackColor = Color.Red;
    button7.BackColor = Color.Red;
}

在 btnNext_Click 我有:

On the btnNext_Click I have:

private void btnNext_Click(object sender, EventArgs e)
    {
        // swap panels
        dPanel.Visible = false;
        qPanel.Visible = true;

        button4.BackColor = SystemColors.Control;
        button5.BackColor = SystemColors.Control;
        button6.BackColor = SystemColors.Control;
        button7.BackColor = SystemColors.Control;

        button4.Font = new Font(button4.Font.Name, button4.Font.Size, FontStyle.Regular);
        button5.Font = new Font(button5.Font.Name, button5.Font.Size, FontStyle.Regular);
        button6.Font = new Font(button6.Font.Name, button6.Font.Size, FontStyle.Regular);
        button7.Font = new Font(button7.Font.Name, button7.Font.Size, FontStyle.Regular);

        if ( qCounter != qSection )
        {
            PickQuestion();
            label5.Text = "Question " + qCounter.ToString() + " of " + qSection.ToString();

            button4.Enabled = true;
            button5.Enabled = true;
            button6.Enabled = true;
            button7.Enabled = true;
        }
        else
        {
            btnNext.Enabled = false;
            label5.Text = "You answered " + qCorrect.ToString() + " questions correctly out of a possible " + qSection.ToString();
        }
    }

非常感谢您对问题的任何帮助.

Any help with the problem would be much much appreciated.

推荐答案

好的,我明白了!在你的回答按钮的事件处理程序中,你总是增加 qCorrect,但你只检查 qCorrect == 1.如果您点击了第一个答案,qCorrect 将为 1,当您第二次回答时,qCorrect 将为 2,但您检查 qCorrect == 1.等等……

Okay, I got it! In your event-handlers for your answer-buttons, you are always incrementing qCorrect, but you only check for qCorrect == 1. If you have clicked the first answer, qCorrect will be 1 and when you answer the second time, qCorrect will be 2, but you check for qCorrect == 1. And so on...

当您在 button4_click 和 button6_click 中为简单模式设置断点时,您就会明白我的意思!希望这可以帮助您进一步.

When you set breakpoints in the button4_click and button6_click for easy-mode, you will see what I mean! Hope this helps you further.

这篇关于根据正确/错误答案更改按钮的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆