开发者

Windows Forms' CheckBox CheckedChanged vs. CheckStateChanged

开发者 https://www.devze.com 2022-12-29 04:16 出处:网络
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.

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?


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.


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);
 }


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).


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.


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 .Checkedreturns what the value IS


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号