Jerry Nixon on Windows: CheckBox.Checked versus CheckBox.CheckState

Friday, February 10, 2006

CheckBox.Checked versus CheckBox.CheckState

We found what I call a bug in the WinForms framework.

Scenario: You have a TextBox and a CheckBox bound to the same BindingSource.

Event: In the CheckedChanged event of the CheckBox you change the Text property of the TextBox.

Affect: The CheckedChanged event fires twice instead of once AND (and this is the bad part) the CheckState of the CheckBox remains the initial state and NOT the new state as a result of you checking it.

Solution: Use the CheckStateChanged event.

Rant: There’s no really clear reason why this works like that. But it does – and at least there is a work around. It also appears that the Checked property and the CheckState property of a Checkbox are redundant. Apparently not, as I learned.

Here’s some further reading:

The summary is that Checked is a two state value (a Boolean of true or false) while CheckState is a three state value (Checked, Unchecked and Indeterminate).

None of this explains why the CheckChanged event handles so badly. I might also add that CheckChanged should not be the default event of a CheckBox if there is some major change coming in the framework – CheckStateChanged should be.