Windows Forms' CheckBox CheckedChanged vs. CheckStateChanged
.NetWinformsEventsControls.Net Problem Overview
Windows Forms' CheckBox
control implements both CheckedChanged
and CheckStateChanged
events. As far as I can tell, both fire when the checked status of the checkbox is changed.
CheckedChanged
precedes CheckStateChanged
, but other than that I see no difference. Am I missing something? Should one be preferred over another?
.Net Solutions
Solution 1 - .Net
CheckState (and thus CheckStateChanged) allow for using a checkbox that can have three values: it can be checked, unchecked or 'indeterminate' - i.e. it has ThreeState set to true.
If you're not using ThreeState, then CheckedChanged is all you need.
Solution 2 - .Net
My guess would be that it has to do with tri-state checkboxes. This is the guts of the CheckState setter:
if (this.checkState != value)
{
bool flag = this.Checked;
this.checkState = value;
if (base.IsHandleCreated)
{
base.SendMessage(0xf1, (int) this.checkState, 0);
}
if (flag != this.Checked)
{
this.OnCheckedChanged(EventArgs.Empty);
}
this.OnCheckStateChanged(EventArgs.Empty);
}
Solution 3 - .Net
The two events are effectively the same unless you set the ThreeState
property to true
. Without having set ThreeState
, both will fire when the check box is checked or unchecked and both will fire after the value has changed.
The main difference is when you do set ThreeState
to true, which adds the Indeterminate
CheckState
:
- The control considers
Indeterminate
to be "checked". (Checked == true
). - Transitioning between
Checked
andUnchecked
is the same as before. - Transitioning between
Checked
andIndeterminate
does not fire theCheckedChanged
event, becauseChecked
staystrue
. - Transitioning between
Unchecked
andIndeterminate
does fire theCheckedChanged
event, becauseChecked
changes fromfalse
totrue
or vice-versa. - Clicking on a three state checkbox, the states transition from
Unchecked
toChecked
toIndeterminate
and back toUnchecked
. You can still transition fromUnchecked
toIndeterminate
programmatically.
(Note the difference between the Checked
property, which is a two state boolean property, and the Checked
state, which is one of the three possible values of the CheckState
property.)
TL;DR: The main practical difference is that the CheckedChanged
event doesn't fire on a three state checkbox when transitioning from CheckState.Checked
to CheckState.Indeterminate
or vice-versa, because both states are considered to be checked (Checked == true
).
Solution 4 - .Net
Not an official answer to the question, but more of a follow-up comment.
I wanted to trigger 2 events when the CheckBox was clicked. In the Designer file, I could duplicate the line adding the event to the CheckedChanged, but as soon as I was modifying something in the Design screen, only the first event would be kept.
My solution was to add one event in CheckedChanged and the other to CheckStateChanged. Both events are now triggered when the CheckBox is clicked.
Solution 5 - .Net
As far as I can tell:
CheckChanged
is fired BEFORE the checked value is changed, so .Checked
returns what the value WAS,
CheckStateChanged
is fired AFTER the checked value is changed, so .Checked
returns what the value IS
Solution 6 - .Net
CheckState fires before the new value is committed. CheckStateChanged fires after the new value is committed.
If your looking for dataset.haschanges to do an update after a checkbox value modification you need to use checkstatechanged event. Just make sure to disable threestate to keep from issues with NULL getting in there.