mardi 9 juin 2020

Winform: How to know if CheckBox has been checked from code or UI

I'm developing a winform c# application. In this application, I'm using Checkbox (JCS.ToggleSwitch to be precise).

I have added a CheckedChanged event to execute some code when the Checkbox has been checked.

I would like to be able to change the state of the checkbox in the code without triggering the code in this CheckedChanged event. In order to do that, I would like to know if it's possible to know if the checkbox state has been changed because of code or because the user clicked on the checkbox.

I tried to detect that with the sender DummyCheckbox_CheckedChanged(object sender, EventArgs e), although, the sender is the same when the state has been changed from a piece of code or by the UI.

Do you know if it's possible to only execute my code when the checkbox has been updated by the user?

 private JCS.ToggleSwitch DummyCheckbox;
 this.DummyCheckbox= new JCS.ToggleSwitch();
 this.DummyCheckbox.CheckedChanged += new JCS.ToggleSwitch.CheckedChangedDelegate(this.DummyCheckbox_CheckedChanged);

 private void DummyCheckbox_CheckedChanged(object sender, EventArgs e)
 {
      if(???) //CheckState has been changed from UI
      {
           if(DummyCheckbox.Checked)
           {
               //Code 1
           }
           else
           {
               //Code 2
           }
      }
      else //CheckState has been changed because of code "DummyCheckbox.Checked = true"
      {
           //Code 3
      }
 }



Aucun commentaire:

Enregistrer un commentaire