Jump to content
Sign in to follow this  
ChrisChuah

TSwitch to allow change?

Recommended Posts

Hi

Is there a function when there is switch event, to disallow the component from switching from off to on?

In my function, when the user activate the OnSwitch event, i will prompt the user to turn off?

if the user said no, then the switch should remain at On. However, now the switch will auto turn off even though i set the IsCheck to true

 

any ideas?

 

regards

chris

Share this post


Link to post
1 hour ago, ChrisChuah said:

Is there a function when there is switch event, to disallow the component from switching from off to on?

Why not just set Enabled to False?

Share this post


Link to post
Guest
1 hour ago, ChrisChuah said:

...

any ideas?

many!

but let's use this my sample to explan a little bit:

 

image.thumb.png.4ffca511f3c22f1aadc4f1601b132b57.png

uses
  FMX.DialogService;

var
  lAnswerYes_or_Not_to_Switch: boolean = false;

procedure TForm2.Switch1Click(Sender: TObject);
begin
  Memo1.Lines.Add('(occurr first than OnSwitch... OnClick ' + DateTimeToStr(now));
  //
  TDialogService.MessageDialog('Let checked or not', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], TMsgDlgBtn.mbYes, 0, { }
    procedure(const AResult: TModalResult)
    begin
      lAnswerYes_or_Not_to_Switch := true;
      //
      if not(AResult = mrYes) then
        lAnswerYes_or_Not_to_Switch := false
    end);
end;

procedure TForm2.Switch1Switch(Sender: TObject);
begin
  //
  // Occurs immediately after you change the state of this switch component from on to off and vice versa. <--- then it should be these event to use!
  //
  // if (Sender as TSwitch).IsChecked then
  if not lAnswerYes_or_Not_to_Switch then
  begin
    Memo1.Lines.Add('OnSwitch im checked!!! ' + DateTimeToStr(now));
    (Sender as TSwitch).IsChecked := false;
  end
  else
    (Sender as TSwitch).IsChecked := true; // be "true" or "false" works, but ... the "Presentation" dont look that, then is necessary re-call it!
  //
  (Sender as TSwitch).ReloadPresentation; // the "magic" to change the screen-presentation is here!
  //
  Memo1.Lines.Add('OnSwitch ' + DateTimeToStr(now));
end;

hug

Edited by Guest

Share this post


Link to post
Guest
3 minutes ago, Dave Nottage said:

Why not just set Enabled to False?

on "Event" itself dont works like he want!

Edited by Guest

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×