Juuri Peeter
Juuri Peeter

Reputation: 131

Two procedures from one button

I was wondering if it's possible to have two functions for one button. For example, I have stringgrid in which there are orders, and with the button I want to sum up all the orders, and with the same button I want to get back to the previous state when the orders weren´t summed up.

if Sender = reduceOrderBTN then
begin
J:=1;
reduceOrderBTN.Caption:= 'Show all';
qryReduceOrders.Close;
qryReduceOrders.Open;
 end;

advOrderGrid.RowCount:= qryReduceOrders.RecordCount + 1;

while NOT qryReduceOrders.Eof do
begin

advOrderGrid.Cells[0, qryReduceOrders.RecNo] := IntToStr(qryReduceOrders.RecNo);
advOrderGrid.Cells[1, qryReduceOrders.RecNo] := qryReduceOrdersProductName.AsString;
advOrderGrid.Cells[2, qryReduceOrders.RecNo] := qryReduceOrdersSpecialWish.AsString;
advOrderGrid.Cells[3, qryReduceOrders.RecNo] := qryReduceOrdersQuantity.AsString;
advOrderGrid.Cells[4, qryReduceOrders.RecNo] := qryReduceOrdersprepTime.AsString;
 advOrderGrid.Repaint;
 qryReduceOrders.next;

end;
if (Sender = reduceOrderBTN) and (J = 1) then
 formShow(nil);
//trying to get back to old state but this doesnt work
end;

Upvotes: 1

Views: 1209

Answers (2)

Dsm
Dsm

Reputation: 6013

Probably the most readable way is to create two TNotifyEvent procedures like this.

procedure TMyForm.OnClickNormal(Sender: TObject);
begin
  DoNormalAction;
  MyButton.Caption := 'Reverse Action';
  MyButton.OnClick := OnClickReverse;
end;

procedure TMyForm.OnClickReverse(Sender: TObject);
begin
  DoReverseAction;
  MyButton.Caption := 'Normal Action';
  MyButton.OnClick := OnClickNormal;
end;

You then set the default action and caption in the design editor.

Upvotes: 5

MikeD
MikeD

Reputation: 637

You could simply use the Tag property of the button to track the state.

If reduceOrderBTN.tag = 0 then begin
  // Sum orders code here
  reduceOrderBTN.tag := 1;
end
else begin
  // Set Previous state code here
  reduceOrderBTN.tag := 0;
end;

Upvotes: 1

Related Questions