开发者

Change Checkbox value without raising event

开发者 https://www.devze.com 2022-12-16 22:19 出处:网络
I want to change Checkbox value without raising the onCheck event. I know there are work arounds like un registering ,changing the value then re- registering,

I want to change Checkbox value without raising the onCheck event. I know there are work arounds like un registering ,changing the value then re- registering, Inside the event method based on flag either skip or evaluating the method statements or any other work arounds. I am looki开发者_C百科ng for a cleaner ways for it.


I know this is an old thread but the answer given is way too much trouble.
I almost never use the OnCheckedChanged event. I prefer to use OnMouseUp instead. That way you can set check boxes or radio buttons like you want them on startup without triggering the events.

In the OnMouseUp event check to see if the box is checked after the mouseUp happens like this..

private void chkBox1_MouseUp(object sender, MouseEventArgs e)
{
   if (chkBox1.Checked)
   {
      // do some stuff if the box is checked after a mouseup on it
   }   
}

I've been doing it this way for a long time. It use to be a big hassle to put flag variables in OnChanged Events to tell functions not to do anything while we are setting the box states. Using MouseUp is way easier. gc


You can derive from CheckBox class, override CheckBox.OnCheckedChanged Method and do not call the base class's OnCheckedChanged method and the event will not be fired. You can also have a property which indicates whether the event should be raised or not.


Remove and Reassign handler:

myCheckbox.Checked -= myCheckbox_Checked;
myCheckbox.IsChecked = true;
myCheckbox.Checked += myCheckbox_Checked;
0

精彩评论

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