隐藏/显示问卷应用程序上的单选按钮

时间:2012-07-03 21:12:42

标签: c# winforms

我正在做一个涉及创建一个简单的测验类型表单应用程序的作业。但是,每当我运行该程序时,只有第一个答案显示多个问题,而我无法为我的生活找出原因。

这是contstructor:

MultipleChoice dlg =
    new MultipleChoice(
        new Question("What is the capital of Zimbabwe?",
            new Answer("Paris", false),
            new Answer("Washington D.C.", false),
            new Answer("Harare", true),
            new Answer("Cairo", false),
            new Answer("N'Djamena", false)));
if (dlg.ShowDialog() == DialogResult.OK)
{
   if (dlg.Correct) MessageBox.Show("You got something right!");
   else MessageBox.Show("You couldn't be more wrong");
}

这是问题表格代码:

private Question Q;
public MultipleChoice (Question q)
{
    Q = q;
    InitializeComponent();
    textPrompt.Text = Q.Prompt;
    if (Q.A != null)
    {
        radioA.Text = Q.A.Prompt;
    }
    else radioA.Hide();
    if (Q.B != null)
    {
        radioB.Text = Q.B.Prompt;
    }
    radioB.Hide();
    if (Q.C != null)
    {
        radioC.Text = Q.C.Prompt;
    }
    radioC.Hide();
    if (Q.D != null)
    {
        radioD.Text = Q.D.Prompt;
    }
    radioD.Hide();
    if (Q.E != null)
    {
        radioE.Text = Q.E.Prompt;
    }
    radioE.Hide();
}
public bool Correct
{
    get
    {
        if (Q == null) return false;
        if (Q.A != null && Q.A.Correct && radioA.Checked) return true;
        if (Q.B != null && Q.B.Correct && radioB.Checked) return true;
        if (Q.C != null && Q.C.Correct && radioC.Checked) return true;
        if (Q.D != null && Q.D.Correct && radioD.Checked) return true;
        if (Q.E != null && Q.E.Correct && radioE.Checked) return true;
        return false;
    }
}

我哪里出错?

1 个答案:

答案 0 :(得分:3)

else之后的任何选项都没有A

if (Q.B != null) 
{ 
    radioB.Text = Q.B.Prompt; 
} 
radioB.Hide();  //This is **always** going to be called - hiding radioB :)

应该是:

if (Q.B != null) 
    radioB.Text = Q.B.Prompt; 
else
    radioB.Hide();