tags:

views:

348

answers:

3

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--;
    }
}
+1  A: 

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.

NotDan
I like the idea of a submit button but I also am using this for learning purposes and want to learn how to use variants. Thanks though.
Lucas McCoy
Um, OK, Tony, but you should first realize that *nothing* in your question or the code you posted as anything to do with variants.
Rob Kennedy
Is there anyway to remove comments ;-)
Lucas McCoy
I think I'm going to do what you said in the first question. Thanks!
Lucas McCoy
+6  A: 

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.

colithium
Your not going to believe this but I did what Rob Kenneday said and I wasted an hour of my life. Thank you so much, I wish I could give you more points for this!
Lucas McCoy
Oh no problem, the way events work in C# is rather mysterious until you're told exactly how they work.
colithium
+5  A: 

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

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

Aligma
Thanks, you were a big help!
Lucas McCoy
I hate to sound like a troll but it does seem like if the Asker was qualified to judge "programming skill" he wouldn't be having so much trouble with this app...
TM
@TM Bit unfair, I think the writers of code complete are judging "programming skill", Lucas is just implementing it as a learning project.. good idea IMHO.
Robert Wagner
The programming skill test is from an excerpt in a book called Code Complete 2nd Edition.
Lucas McCoy
@TM, although I understand that the questioner is not claiming to be an expert, the irony is just too AWESOME to let go without comment--thanks.
Bill K