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 and Unchecked is the same as before.
  • Transitioning between Checked and Indeterminate does not fire the CheckedChanged event, because Checked stays true.
  • Transitioning between Unchecked and Indeterminate does fire the CheckedChanged event, because Checked changes from false to true or vice-versa.
  • Clicking on a three state checkbox, the states transition from Unchecked to Checked to Indeterminate and back to Unchecked. You can still transition from Unchecked to Indeterminate 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBojanGView Question on Stackoverflow
Solution 1 - .NetstuartdView Answer on Stackoverflow
Solution 2 - .NetJacob GView Answer on Stackoverflow
Solution 3 - .NetDave CousineauView Answer on Stackoverflow
Solution 4 - .NetMarkyView Answer on Stackoverflow
Solution 5 - .NetJohn LineView Answer on Stackoverflow
Solution 6 - .NetTWoodView Answer on Stackoverflow