编程技巧测试员(问题)

时间:2009-02-04 05:07:25

标签: c# winforms

我正在研究一个程序,该程序将根据代码完成第2版中的32个主题,告诉程序员在初级,中级或专家级别。我正在使用32个复选框和一个方法来判断哪些被点击。问题是当我检查check复选框属性是否等于true时,它会在复选框实际检查之前得到结果。这是我的所有源代码(到目前为止):

public partial class Main : Form
{
    private int baseScore = 0;

    public Main()
    {
        InitializeComponent();
    }

    private void buttonCalculateScore_Click(object sender, EventArgs e)
    {
        DetermineLevelOfProgrammer();
    }

    private void DetermineLevelOfProgrammer()
    {
        if ((baseScore >= 0) || (baseScore <= 14))
        {
            labelYourScore.Text += " " + baseScore.ToString();
            labelDescription.Text = "You are a beginning programmer, probably in your first year of computer \n"+
                                    "science in school or teaching yourself your first programming language. ";
        }

        // Do the other checks here!

    }

    // If checkbox is checked then increment base score,
    // otherwise decrement base score.
    private void checkBoxVariant_CheckedChanged(object sender, EventArgs e)
    {
        if (checkBoxVariant.Checked)
            baseScore++;
        else
            baseScore--;
    }
}

3 个答案:

答案 0 :(得分:6)

我不确定checkBoxVariant是什么,但......

我认为问题是checkBoxVariant只是32个CheckBox中的一个。我假设您将所有32个CheckChanged事件连接到checkBoxVariant_CheckedChanged方法。

它应该是什么样的:

// If checkbox is checked then increment base score,
// otherwise decrement base score.
private void checkBoxVariant_CheckedChanged(object sender, EventArgs e)
{
   if (((CheckBox)sender).Checked)
      baseScore++;
   else
      baseScore--;
}

sender是一个Object,它指向导致事件被引发的实际Object。因为任何东西都可以引发事件,所以它只是一个必须强制转换为CheckBox的对象。

答案 1 :(得分:6)

if((baseScore&gt; = 0)||(baseScore&lt; = 14))

小心 - 这总是评估为真。您可能打算使用&amp;&amp;。

答案 2 :(得分:1)

CheckedChanged事件只应在检查后触发,所以我不认为这是你的问题。但是,也许你应该在点击提交按钮后检查所有复选框,然后它可能会更简单。