Changing the TControl Enabled activates the click event alone

0

Talk to people,

By changing the (TButton) button to disabled within an event, it triggers another event.

procedure TFormulario.OnButtonExecutarOperacaoClick(Sender: TObject);
begin
  //...
  //aqui está o problema
  //a função abaixo é sincrona assim ele trava nesta linha porque chamou o evento denovo
  ButtonCancelar.Enabled:=true;//.Visible também tem o mesmo comportamento

  FuncaoQueNaoChamaPorCausaDesteProblema();
end;

Without this:

  

ButtonCancel.Enabled: = true

it works normally.

EDIT 1 : control.inc

procedure TControl.SetEnabled(Value: Boolean);  
begin
  if FEnabled <> Value
  then begin
    EnabledChanging; //aqui chama o evento do OnButtonExecutarOperacaoClick
    FEnabled := Value;
    Perform(CM_ENABLEDCHANGED, 0, 0);
    EnabledChanged;
  end;
end;

EDIT 2 I discovered that this only happens when the button starts with enable or visible false. Now the resolution still has no idea

EDIT 3

procedure TControl.EnabledChanging;
begin
  DoCallNotifyHandler(chtOnEnabledChanging);
end;

procedure TControl.DoCallNotifyHandler(HandlerType: TControlHandlerType);
begin
  FControlHandlers[HandlerType].CallNotifyEvents(Self);
end;

EDIT 4

button button B and pressing the A-button will only disable it, even if the function will disable both.

    
asked by anonymous 12.04.2018 / 23:25

2 answers

0

I solved using TTask.Run, after a while I discovered that I had an async function inside the OnClick, so when I called the "FunctionQueNoChamaPause" ("

procedure TForm1.FuncaoQueNaoChamaPorCausaDesteProblema();
begin
 TTask.Run(procedure
  begin
    //...

    DesabilitarBotoes();
  end;
end;

procedure TForm1.DesabilitarBotoes();
begin
  TThread.Synchronize(nil, procedure
  begin
    //...
    ButtonCancelarOperacao.Enabled:= false;
  end);
end;
    
17.04.2018 / 16:33
2

This is not the default behavior. Possibly, your code is making some recursive calls. In this case, you can temporarily disable the event handler:

procedure TFormulario.OnButtonExecutarOperacaoClick(Sender: TObject); begin ButtonCancelar.OnClick := nil; //verificar exatamente qual seria o evento recursivo try ButtonCancelar.Enabled := true; finally ButtonCancelar.OnClick := ButtonCancelarClick; end; FuncaoQueNaoChamaPorCausaDesteProblema(); end;

    
13.04.2018 / 14:24