如何在提交互斥复选框后更新标签
本文关键字:复选框 更新 标签 交互 提交 | 更新日期: 2023-09-27 18:02:18
我正在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";
}
这是一种方法。在您的代码片段中存在未解释的条件。这段代码通过假设最坏的情况来避免这种情况,并且只在发生正确情况时设置正确的值(即答案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;