Deleting Buttons from TPanel, Firemonkey Edition

  

Using Delphi 10.2 (under Windows 10 “19H2”), I can create a new app, drop a single panel on it, and an action list with two items. Both items call the same routine whose purpose is to remove any buttons on the panel, and then add the new ones in:

procedure TForm1.CreateNavPanelButtons(Action: TAction);
begin
NavPanel.RemoveObject(Btn);
Btn.DisposeOf; //problem line

Btn := MakeButton(Action);
NavPanel.AddObject(Btn);
end;

(I’ve simplified to just use one button here.) Remove the existing button, add the new button in. If I call DisposeOf (to free up the button’s memory), the Window object becomes unresponsive (can’t resize, move, close) until I shift focus away from the app.

I’ve included the entire code below:

unit Unit1;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Controls.Presentation, System.Actions, FMX.ActnList;

type
TForm1 = class(TForm)
NavPanel: TPanel;
ActionList: TActionList;
acNextMenu: TAction;
acBackToMainMenu: TAction;
procedure FormCreate(Sender: TObject);
procedure acNextMenuExecute(Sender: TObject);
private
{ Private declarations }
public
Btn: TButton;
procedure CreateNavPanelButtons(Action: TAction);
end;

var
Form1: TForm1;

implementation

{$R *.fmx}

function MakeButton(A: TAction): TButton;
begin
Result := TButton.Create(nil);
Result.Action := A;
Result.Text := (A as TAction).Text;
end;

procedure TForm1.acNextMenuExecute(Sender: TObject);
begin
CreateNavPanelButtons(acBackToMainMenu);
end;

procedure TForm1.CreateNavPanelButtons(Action: TAction);
begin
NavPanel.RemoveObject(Btn);
Btn.DisposeOf;

Btn := MakeButton(Action);
NavPanel.AddObject(Btn);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
CreateNavPanelButtons(acNextMenu);
end;

end.

Here’s the form:

object Form1: TForm1
Left = 0
Top = 0
Caption = ‘Form1’
ClientHeight = 480
ClientWidth = 640
FormFactor.Width = 320
FormFactor.Height = 480
FormFactor.Devices = [Desktop]
OnCreate = FormCreate
DesignerMasterStyle = 0
object NavPanel: TPanel
Align = Top
Size.Width = 640.000000000000000000
Size.Height = 73.000000000000000000
Size.PlatformDefault = False
TabOrder = 0
end
object ActionList: TActionList
Left = 392
Top = 192
object acNextMenu: TAction
Category = ‘Navigation’
Text = ‘NextMenu’
OnExecute = acNextMenuExecute
end
object acBackToMainMenu: TAction
Category = ‘Navigation’
Text = ‘Back To &Main Menu’
OnExecute = FormCreate
end
end
end

Comments are closed.