Peter Turner
Peter Turner

Reputation: 11435

Execute action for automatically unchecked button in Delphi

I have one action I want to perform when a TSpeedButton is pressed and another I want to perform when the same button is "unpressed". I know there's no onunpress event, but is there any easy way for me to get an action to execute when a different button is pressed?

procedure ActionName.ActionNameExecute(Sender: TObject);
begin
  PreviousActionName.execute(Sender);
  //
end;

Seems too clunky.

Upvotes: 3

Views: 3342

Answers (2)

Toon Krijthe
Toon Krijthe

Reputation: 53366

There is no unpress, but you can query the Down property.

The example took some dirty casts, but it works both for the action and for the OnClick.

procedure Form1.ActionExecute(Sender: TObject);
var
  sb : TSpeedButton;
begin
  if Sender is TSpeedButton then
    sb := TSpeedButton(Sender)
  else if (Sender is TAction) and (TAction(Sender).ActionComponent is TSpeedButton) then
    sb := TSpeedButton(TAction(Sender).ActionComponent)
  else 
    sb := nil;

  if sb=nil then
    DoNormalAction(Sender)
  else if sb.Down then
    DoDownAction(sb)
  else 
    DoUpAction(sb);
end;

Upvotes: 5

Francesca
Francesca

Reputation: 21640

From what you describe, I suppose you use your speedbutton with a GroupIndex <>0 but no other buttons in the same group, or at least not working as RadioButtons (AllowAllUp True).

You only have 1 onClick event for pressing the button, but what to do depends on the state of the button if it has a GroupIndex.
So, you have to test for Down being False in your onClick event handler, as Down is updated before the onClick Handler is fired.

ex:

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  with Sender as TSpeedButton do
  begin
    if Down then
      showmessage('pressing')
    else
      showmessage('unpressing');
  end;
end;

Upvotes: 5

Related Questions