c# – Programming Skill Tester (Problem) – Education Career Blog

I’m working on a program that will tell what level a programmer is at beginner, intermediate, or expert based on 32 subjects from a test in Code Complete 2nd Edition. I’m using 32 check boxes and one method to tell which ones are clicked. The problem is that when I check to see if the check boxes checked property is equal to true, it gets the result before the check box actually becomes checked. Here is all of my source code (so far):

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--;
    }
}

,

I’m not sure what checkBoxVariant is exacty but…

I think the problem is that checkBoxVariant is just 1 of the 32 CheckBoxes. I’m assuming you wired all 32 CheckChanged events to the checkBoxVariant_CheckedChanged method.

What it should look like is:

// 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 is an Object that points to the actual Object that caused the event to be raised. Since anything could raise the event, it’s just an Object that must be cast to a CheckBox.

,

if ((baseScore >= 0) || (baseScore <= 14))

Be careful – this will always evaluate to true. You may have intended to use &&.

,

The CheckedChanged event should only fire after it has been checked, so I dont think that is your problem. However, maybe you should just check all check boxes after a submit button click, then it may be simpler.

Leave a Comment