.net Windows 窗体的 CheckBox CheckedChanged 与 CheckStateChanged
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2782566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows Forms' CheckBox CheckedChanged vs. CheckStateChanged
提问by BojanG
Windows Forms' CheckBoxcontrol implements both CheckedChangedand CheckStateChangedevents. As far as I can tell, both fire when the checked status of the checkbox is changed.
Windows 窗体的CheckBox控件同时实现CheckedChanged和CheckStateChanged事件。据我所知,当复选框的选中状态发生变化时,两者都会触发。
CheckedChangedprecedes CheckStateChanged, but other than that I see no difference. Am I missing something? Should one be preferred over another?
CheckedChanged先于CheckStateChanged,但除此之外,我看不出有什么区别。我错过了什么吗?一个应该优先于另一个吗?
采纳答案by Jacob G
My guess would be that it has to do with tri-state checkboxes. This is the guts of the CheckState setter:
我的猜测是它与三态复选框有关。这是 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);
}
回答by stuartd
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 ThreeStateset to true.
CheckState(以及 CheckStateChanged)允许使用具有三个值的复选框:它可以被选中、未选中或“不确定”——即它的ThreeState设置为 true。
If you're not using ThreeState, then CheckedChanged is all you need.
如果您不使用 ThreeState,那么 CheckedChanged 就是您所需要的。
回答by Dave Cousineau
The two events are effectively the same unless you set the ThreeStateproperty to true. Without having set ThreeState, both will fire when the check box is checked or unchecked and both will fire afterthe value has changed.
除非您将ThreeState属性设置为 ,否则这两个事件实际上是相同的true。如果没有设置ThreeState,则在选中或取消选中复选框时两者都会触发,并且在值更改后两者都会触发。
The main difference is when you do set ThreeStateto true, which adds the IndeterminateCheckState:
主要区别在于当您设置ThreeState为 true 时,它会添加IndeterminateCheckState:
- The control considers
Indeterminateto be "checked". (Checked == true). - Transitioning between
CheckedandUncheckedis the same as before. - Transitioning between
CheckedandIndeterminatedoes notfire theCheckedChangedevent, becauseCheckedstaystrue. - Transitioning between
UncheckedandIndeterminatedoesfire theCheckedChangedevent, becauseCheckedchanges fromfalsetotrueor vice-versa. - Clicking on a three state checkbox, the states transition from
UncheckedtoCheckedtoIndeterminateand back toUnchecked. You can still transition fromUncheckedtoIndeterminateprogrammatically.
- 控件认为
Indeterminate已“检查”。(Checked == true). - 在
Checked和之间的转换与Unchecked以前相同。 - 之间的转换
Checked,并Indeterminate没有不火的CheckedChanged事件,因为Checked住宿true。 - 在
Unchecked和之间转换Indeterminate确实会触发CheckedChanged事件,因为Checked从false到true或反之亦然。 - 点击一个三个态复选框,从美国过渡
Unchecked到Checked以Indeterminate再换Unchecked。您仍然可以Unchecked以Indeterminate编程方式从 过渡到。
(Note the difference between the Checkedproperty, which is a two state boolean property, and the Checkedstate, which is one of the three possible values of the CheckStateproperty.)
(注意Checked属性是两个状态的布尔属性,Checked状态是CheckState属性的三个可能值之一。)
TL;DR: The main practical difference is that the CheckedChangedevent doesn't fire on a three state checkbox when transitioning from CheckState.Checkedto CheckState.Indeterminateor vice-versa, because both states are considered to be checked (Checked == true).
TL;DR:主要的实际区别是,在CheckedChanged从CheckState.Checkedto转换CheckState.Indeterminate或反之亦然转换时,事件不会在三态复选框上触发,因为这两种状态都被视为已选中 ( Checked == true)。
回答by John Line
As far as I can tell:
据我所知:
CheckChangedis fired BEFORE the checked value is changed, so .Checkedreturns what the value WAS,
CheckChanged在检查值更改之前被触发,因此.Checked返回值是什么,
CheckStateChangedis fired AFTER the checked value is changed, so .Checkedreturns what the value IS
CheckStateChanged在检查值更改后触发,因此.Checked返回值是什么
回答by TWood
CheckState fires before the new value is committed. CheckStateChanged fires after the new value is committed.
CheckState 在提交新值之前触发。CheckStateChanged 在提交新值后触发。
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.
如果您正在寻找 dataset.haschanges 在复选框值修改后进行更新,您需要使用 checkstatechanged 事件。只需确保禁用三态以防止出现 NULL 问题。

