Greg
Greg

Reputation: 157

C# - Checkbox's CheckChanged Event Has Wrong Checked State

I'm working on a Windows form which should enable / disable some controls based on a checkbox's checked status. To watch for this, there is an event handler for the CheckedChanged event. This has worked for other forms just fine, with the same exact code, but it is not working correctly here. Whether the checkbox is being checked or unchecked, the checked state is always false. Here's the code:

    private void chkDisable_CheckedChanged(object sender, EventArgs e)
    {
        if (chkDisable.Checked)
        {
            DisableFormFields();
        }
        else
        {
            EnableFormFields();
        }
    }

Like I said, this exact code works fine in one form -- the Checked state is correctly set to true or false based on what you just changed it to. But in the new form, the Checked state is always false in that method, whether it is being checked or unchecked. The event fires fine, and hits the breakpoint I set in the method, but the Checked state never changes. Any advice?

Thanks!

Upvotes: 0

Views: 5725

Answers (2)

Marshal
Marshal

Reputation: 6651

Check that your eventHandler chkDisable_CheckedChanged is applied to correct checkBox i.e. to chkDisable checkbox.

If still the problem persits, delete and re-drag the checkbox control and apply the event.

Applying casting and shortcut should not be the correct methodology to solve a problem (This is what I personally think.)

Upvotes: 3

Likurg
Likurg

Reputation: 2760

Try this

private void chkDisable_CheckedChanged(object sender, EventArgs e)
{
        if (((CheckBox)sender).Checked)
        {
            DisableFormFields();
        }
        else
        {
            EnableFormFields();
        }
}

Upvotes: 4

Related Questions