FlowPanels (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the use of Flow panel to automatically align a set of controls and the different methods that can be employed for achieving this. A Stack is used to control the order in which buttons are destructed.

Code

procedure TForm2.btAButtonClick(Sender: TObject);
var
  ThisButton : TButton;
  LastButton : TButton;
  Temp       : String;
begin
  { Since the sender is a TButton, cast it. }
  ThisButton := Sender as TButton;

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

    { Change the captions of this button, including 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
  { Change the flow order (rotate it). }
  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, push 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;

Uses