提交互斥复选框后如何更新标签?

时间:2013-03-05 01:51:05

标签: c# asp.net

我正在进行C#编码,单击提交按钮时遇到问题,选择几个不同的复选框答案组合,它们没有正确更新“正确”或“不正确”。如果选中仅选择Answer2和Answer3,如何仅显示“正确”标签,如何将代码调整到哪里?即使用正确的答案选择了一个错误答案,也会显示“不正确”。

if (Answer2.Checked && Answer3.Checked == true)
    {
        lblQuestionResult4.ForeColor = System.Drawing.Color.Green;
        lblQuestionResult4.Text = "Correct";
    }
    else
    {
        if (Answer1.Checked && Answer4.Checked == true)
        {
            lblQuestionResult4.ForeColor = System.Drawing.Color.Red;
            lblQuestionResult4.Text = "Incorrect";
        }

1 个答案:

答案 0 :(得分:0)

这是一种方法。在您的代码段中,有些条件无法解释。该代码通过假设最坏情况避免了这种情况,并且仅在正确情况下设置值(即,检查答案2和3,并且所有其余部分都未被检查)。

var color = System.Drawing.Color.Red; //assume incorrect answer
var label = "Incorrect";

if (Answer2.Checked && Answer3.Checked && !Answer1.Checked && !Answer4.Checked)
{
  //only the 2 correct answers have been checked
  color = System.Drawing.Color.Green;
  label = "Correct";
}

// set the controls
lblQuestionResult4.ForeColor = color;
lblQuestionResult4.Text = label;