RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TObjectStack.Push Method

Adds an object to the top of the stack.

Pascal
function Push(AObject: TObject): TObject;
C++
__fastcall TObject * Push(TObject * AObject);

Call Push to add an object to the stack. 

AObject is the object to add to the beginning of the stack. 

Push returns a reference to the object that was just added (AObject).  

C++ Examples: 

 

//
//This example demonstrates the use of Flow panel to automatically
//align a set of controls and the different methods that can be used for that.
//A Stack is used to control the order of destruction of buttons.
//
void __fastcall TForm2::btAButtonClick(TObject *Sender)
{
    TButton *thisButton;
    TButton *lastButton;
    String temp;

    /* Sender is a TButton, cast it */
    thisButton = dynamic_cast<TButton*>(Sender);

    if (m_Stack->Count() > 0)
    {
        /* If there are buttons in stack, peek it */
        lastButton = dynamic_cast<TButton*>(m_Stack->Peek());

        /* Change the captions of this button and the last one */
        temp = thisButton->Caption;
        thisButton->Caption = lastButton->Caption;
        lastButton->Caption = temp;
    }
}

void __fastcall TForm2::btAddButtonClick(TObject *Sender)
{
    /* Create a new button */
    TButton *button = new TButton(m_FlowPane);

    button->Parent = m_FlowPane;
    button->Caption = IntToStr(m_Stack->Count());
    button->OnClick = btAButtonClick;

    /* Push the button to the stack */
    m_Stack->Push(button);
}

void __fastcall TForm2::btReArrangeClick(TObject *Sender)
{
    /* Change the current flow style (rotate them) */
    TFlowStyle newFlowStyle = m_FlowPane->FlowStyle;

    if (newFlowStyle == fsBottomTopRightLeft)
        newFlowStyle = fsLeftRightTopBottom;
    else
        newFlowStyle = newFlowStyle + 1;

    m_FlowPane->FlowStyle = newFlowStyle;
}

void __fastcall TForm2::btRemoveButtonClick(TObject *Sender)
{
    TButton *button;

    if (m_Stack->Count() > 0)
    {
        /* if the stack is not empty, pop the last button */
        button = dynamic_cast<TButton*>(m_Stack->Pop());
        delete button;
    }
}

void __fastcall TForm2::FormCreate(TObject *Sender)
{
    /* Create a new TFlowPanel pane and align it to client */
    m_FlowPane = new TFlowPanel(this);
    m_FlowPane->Parent = this;
    m_FlowPane->Align = alClient;

    /* Set the initial flowing style */
    m_FlowPane->FlowStyle = fsLeftRightTopBottom;

    /* Create the button stack */
    m_Stack = new TObjectStack();
}

void __fastcall TForm2::FormDestroy(TObject *Sender)
{
    /* Destroy the stack also */
    delete m_Stack;
}

 

Delphi Examples: 

{
This example demonstrates the use of Flow panel to automatically
align a set of controls and the different methods that can be used for that.
A Stack is used to control the order of destruction of buttons.
}
procedure TForm2.btAButtonClick(Sender: TObject);
var
  ThisButton : TButton;
  LastButton : TButton;
  Temp       : String;
begin
  { Sender is a TButton, cast it }
  ThisButton := Sender as TButton;

  if FStack.Count > 0 then
  begin
    { If there are buttons in stack, peek it }
    LastButton := FStack.Peek() as TButton;

    { Change the captions of this button and the last one }
    Temp := ThisButton.Caption;
    ThisButton.Caption := LastButton.Caption;
    LastButton.Caption := Temp;
  end;
end;

procedure TForm2.btAddButtonClick(Sender: TObject);
var
  Button : TButton;
begin
  { Create a new button }
  Button := TButton.Create(FFlowPane);
  Button.Parent := FFlowPane;
  Button.Caption := IntToStr(FStack.Count);
  Button.OnClick := btAButtonClick;

  { Push the button to the stack }
  FStack.Push(Button);
end;

procedure TForm2.btReArrangeClick(Sender: TObject);
var
  NewFlowStyle : TFlowStyle;
begin
  { Chnage the flow order (rotate) }
  NewFlowStyle := FFlowPane.FlowStyle;

  if NewFlowStyle = fsBottomTopRightLeft then
     NewFlowStyle := fsLeftRightTopBottom
  else
    Inc(NewFlowStyle);

  FFlowPane.FlowStyle := NewFlowStyle;
end;

procedure TForm2.btRemoveButtonClick(Sender: TObject);
var
  Button : TButton;
begin
  if FStack.Count > 0 then
  begin
    { if the stack is not empty, pop the last button }
    Button := FStack.Pop() as TButton;
    Button.Free();
  end;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  { Create a new TFlowPanel pane and align it to client }
  FFlowPane := TFlowPanel.Create(Self);
  FFlowPane.Parent := Self;
  FFlowPane.Align := alClient;

  { Set the initial flowing style }
  FFlowPane.FlowStyle := fsLeftRightTopBottom;

  { Create the button stack }
  FStack := TObjectStack.Create();
end;

procedure TForm2.FormDestroy(Sender: TObject);
begin
  { Destroy the stack also }
  FStack.Free();
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!