views:

847

answers:

2

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.

+5  A: 

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;
Gamecat
+4  A: 

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;
François
@Francois, In this case, Sender is an action, so the Sender as TSpeedButton raises an exception.
Gamecat