Link to home
Start Free TrialLog in
Avatar of Claudio Da Mata
Claudio Da Mata

asked on

Double click cancellation causes a button event overwrite

I have the code:

Type
  TButton = Class (stdctrls.TButton)
  protected
    procedure Click; Override;
  end;

Procedure TButton.Click;
Begin
  Enabled := False;
  Inherited;
  Enabled := True;
End;

(This stops a user from making a ButtonClick event happen twice if they double click on a button)

My problem is: A few of my buttons have to disable themself if a certain case is met, but the double click cancellation procedure I added re-enables them after they disable themself. I know why this happens but is there any way of preventing this?

Thanks.
Claudio
Avatar of Geert G
Geert G
Flag of Belgium image

does this work ?
enable/disable schedules a redraw of the control (via invalidate)

i'm guessing you need push/pop stack solution for disabling
Avatar of Claudio Da Mata
Claudio Da Mata

ASKER

I'm a fairly new programmer, could you possibly explain your last comment?
i was testing with this code:
unit unitMovePanels;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
  TButton = Class (Vcl.StdCtrls.TButton)
  private
    fDisabledStack: Integer;
    function GetEnabledStack: boolean;
    procedure SetEnabledStack(const Value: boolean);
  protected
    procedure Click; Override;
    property Enabled: boolean read GetEnabledStack write SetEnabledStack;
  end;

  TfrmMovePanels = class(TForm)
    Button2: TButton;
    Button1: TButton;
    Button3: TButton;
    Button4: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    procedure AddMsg(Msg: string);
  public
    { Public declarations }
  end;

var
  frmMovePanels: TfrmMovePanels;

implementation

{$R *.dfm}

procedure TfrmMovePanels.AddMsg(Msg: string);
begin
  Memo1.Lines.Add(Msg);
end;

procedure TfrmMovePanels.Button1Click(Sender: TObject);
begin
  Button2.Enabled := True;
end;



Procedure TButton.Click;
Begin
  Enabled := False;
  try
    inherited;
  finally
    Enabled := True;
  end;
End;


function TButton.GetEnabledStack: boolean;
begin
  Result := fDisabledStack <= 0;
end;

procedure TButton.SetEnabledStack(const Value: boolean);
begin
  inherited Enabled := Value;
  if Value then
  begin
    Dec(fDisabledStack);
    if fDisabledStack < 0 then
      fDisabledStack := 0;
  end
    else Inc(fDisabledStack);
  frmMovePanels.AddMsg('Button: ' + Name  + ', Enabled = ' + IntToStr(Integer(Value)) + ', DisabledStack = ' + IntToStr(fDisabledStack));
end;

procedure TfrmMovePanels.Button2Click(Sender: TObject);
begin
  Button3.Enabled := False;
end;

procedure TfrmMovePanels.Button4Click(Sender: TObject);
begin
  Button4.Enabled := True;
end;

end.

Open in new window

I use TAction assigned to buttons - and in OnUpdate event of TAction - I set enable state depending on some conditions - this works great...
it's possible to move all control state to 1 procedure
delphi will even call this procedure automatically when it's the right procedure
type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject); 
    procedure Button2Click(Sender: TObject); 
    Button3: TButton;
  private
    fTest: Integer;
  protected
    procedure UpdateActions; override;
  end;

procedure TForm1.Button1Click(Sender: TObject); 
begin
  fTest := fTest + 1;
end;

procedure TForm1.Button2Click(Sender: TObject); 
begin
  fTest := fTest -1;
end;

procedure TForm1.UpdateActions;
begin
  Button3.Enabled := (fTest mod 3 = 0);
end;

Open in new window


see ?
no call to UpdateActions, yet Button3 will disable or enabled based on the value of fTest
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.