Search code examples
androiddelphifiremonkey

Avoid TSwitch's OnSwitch Event in Firemonkey


When setting TSwitch's IsChecked property programatically, the event OnSwitch getting called. How to avoid this? I would like the OnSwitch event to be called only in case of user interaction, when the user switch the control by clicking.


Solution

  • With OnSwitch = Switch1Switch() and the following button OnClick just simulating the programmatic change, you can do the following:

    procedure TForm29.Button1Click(Sender: TObject);
    begin
      Switch1.OnSwitch := nil;
      Switch1.IsChecked := not Switch1.IsChecked;
      Switch1.OnSwitch := Switch1Switch;
    end;
    

    This changes the state of the switch and fires OnSwitch when user flips the switch, but not when state is changed programmatically.