36

How do I prevent firing CheckedChanged event when checking a control programmatically?

I usually do this the following way.

private bool isFrozen = false;

private void btn1_CheckedChanged(object sender, EventArgs e)
{
    if (isFrozen) 
        return;

    isFrozen = true;
    btn2.Checked = false;
    isFrozen = false;

    // Do some stuff
}

private void btn2_CheckedChanged(object sender, EventArgs e)
{
    if (isFrozen) 
        return;

    isFrozen = true;
    btn1.Checked = false;
    isFrozen = false;

    // Do another stuff
}

Is there a better or more common solution?

1
  • 1
    This is the proper way. Technically you should set isFrozen back to false in a finally block. Commented Nov 11, 2011 at 7:19

4 Answers 4

62

I think your way is fine.

The other way to do it is remove the EventHandler before the check, and then add it back again after the check. This way eliminates the need for the isFrozen variable.

private void btn1_CheckedChanged(object sender, EventArgs e)
{
  btn2.CheckedChanged -= btn2_CheckedChanged;
  btn2.Checked = false;
  btn2.CheckedChanged += btn2_CheckedChanged;

    // Do some staff
}

private void btn2_CheckedChanged(object sender, EventArgs e)
{
  btn1.CheckedChanged -= btn1_CheckedChanged;
  btn1.Checked = false;
  btn1.CheckedChanged += btn1_CheckedChanged;

    // Do another staff
}
7
  • Good idea. I have never thought about removing the events.
    – Zenya
    Commented Nov 13, 2011 at 2:21
  • 3
    This should be marked as the correct answer. Even if there's no noticeable difference in how this functions vs the OP's approach, this expresses intent is a very clear way, so the next person who works on this code can clearly understand what is being done and why, even without comments to explain it.
    – Jim
    Commented Jul 13, 2015 at 17:25
  • how about when you don't know what is the slot bound to the signal ? and that is supposing that there is only one. i'd prefer a "swap" method on the event. var e = new typeof(btn.Changed); e.Swap(btn.Changed); btn.Checked = false; e.Swap(btn.Changed);
    – v.oddou
    Commented Mar 22, 2016 at 7:24
  • @v.oddou It's not very clear where this var e = new typeof(btn.Changed); is coming from. btn.Changed? e.Swap?
    – LarsTech
    Commented Mar 22, 2016 at 13:24
  • @LarsTech you cannot do this in C#, I was raising the issue that events are not first class citizen. This is bad in my opinion. They should be assignable, constructible, and swappable, like boost signal. the new typeof(btn.Changed) was just an imaginary way to create an empty event which when temporarily assigned to btn.Changed would not cause the attached slots to fire.
    – v.oddou
    Commented Mar 23, 2016 at 13:05
8

In VB:

RemoveHandler btn2.CheckedChanged, AddressOf btn2_CheckedChanged
btn2.Checked = false
AddHandler btn2.CheckedChanged, AddressOf btn2_CheckedChanged
3
  • 4
    The answer was about C# not VB. Commented Apr 15, 2015 at 16:00
  • 8
    But it's useful for some of us who thought of how to do this in C# (our first language), but had to write the code in VB.net (client's request). Commented Apr 21, 2016 at 20:22
  • 1
    While I very much appreciated this answer, I think it should be added as a comment to the accepted answer.
    – Eric
    Commented Jun 24, 2016 at 16:58
7

I came across this post after wanting to implement something like this for a while. I regularly use Measurement Studio from National Instruments, and their WinForms controls that have the event StateChanging or StateChanged pass a parameter of type ActionEventArgs, which has a property Action which can take three values: ByKeyboard, ByMouse and Programatic. This is very useful in determining what has caused the state of the control to change. I wanted to replicate this in a standard WinForms checkbox.

Here is my code:

public enum ControlSource
{
    Programatic,
    ByKeyboard,
    ByMouse
}

public class AwareCheckBox : Checkbox
{
    public AwareCheckBox()
            : base()
    {
        this.MouseDown += AwareCheckbox_MouseDown;
        this.KeyDown += AwareCheckbox_KeyDown;
    }

    private ControlSource controlSource = ControlSource.Programatic;

    void AwareCheckbox_KeyDown(object sender, KeyEventArgs e)
    {
        controlSource = ControlSource.ByKeyboard;
    }

    void AwareCheckbox_MouseDown(object sender, MouseEventArgs e)
    {
        controlSource = ControlSource.ByMouse;
    }

    public new event AwareControlEventHandler CheckedChanged;
    protected override void OnCheckedChanged(EventArgs e)
    {
        var handler = CheckedChanged;
        if (handler != null)
            handler(this, new AwareControlEventArgs(controlSource));

        controlSource = ControlSource.Programatic;
    }
}

public delegate void AwareControlEventHandler(object source, AwareControlEventArgs e);

public class AwareControlEventArgs : EventArgs
{
    public ControlSource Source { get; private set; }

    public AwareControlEventArgs(ControlSource s)
    {
        Source = s;
    }
}

I'm sure there are improvements to make, but my rudimentary testing has demonstrated that it works. I have posted here simply in case others stumble across this issue and want a clearer way of distinguishing where the change was initiated. Any comments welcome.

1
  • 1
    Thanks for positing this Nick. I was trying to remember how to do it in Measurement Studio when I came across this. Commented Jun 23, 2020 at 19:21
0

Just have a counter value set and check for the value in the beginning of the event. It solved my problem in 10 minutes. I am using 5 slide buttons in Xamarin to make it as a radio button.

 private void testtoggle1(object sender, ToggledEventArgs e)
        {
            if (chk_ctr == 1) { return; }
            chk_ctr = 1;
            sw2.IsToggled= false;
            sw3.IsToggled = false;
            sw4.IsToggled = false;
            sw5.IsToggled = false;
            chk_ctr = 0;
        }
        private void testtoggle2(object sender, ToggledEventArgs e)
        {
            if (chk_ctr == 1) { return; }
            chk_ctr = 1;
            sw1.IsToggled = false;
            sw3.IsToggled = false;
            sw4.IsToggled = false;
            sw5.IsToggled = false;
            chk_ctr = 0;

        }
        private void testtoggle3(object sender, ToggledEventArgs e)
        {
            if (chk_ctr == 1) { return; }
            chk_ctr = 1;
            sw1.IsToggled = false;
            sw2.IsToggled = false;
            sw4.IsToggled = false;
            sw5.IsToggled = false;
            chk_ctr = 0;
        }
        private void testtoggle4(object sender, ToggledEventArgs e)
        {
            if (chk_ctr == 1) { return; }
            chk_ctr = 1;
            sw1.IsToggled = false;
            sw2.IsToggled = false;
            sw3.IsToggled = false;
            sw5.IsToggled = false;
            chk_ctr = 0;
        }
        private void testtoggle5(object sender, ToggledEventArgs e)
        {
            if (chk_ctr == 1) { return; }
            chk_ctr = 1;
            sw1.IsToggled = false;
            sw2.IsToggled = false;
            sw3.IsToggled = false;
            sw4.IsToggled = false;
            chk_ctr = 0;
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.