Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically create and remove a label from a form

I got some code here, which creates a label just fine, but when I free the label it STILL shows on the form. Even though it's been removed and no longer 'assigned'.

Here is the code below. It creates the label fine, but wont remove. No exceptions, and the assigned says false.

I can reproduce this with a TRectangle as well.

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
      Lab : TLabel;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.Button1Click(Sender: TObject);
begin
  Lab := TLabel.Create(Self);
  Lab.Parent := Form1;
  Lab.Text := 'Hello!';
  Lab.Position.X := 30;
  Lab.Position.Y := 40;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  FreeAndNil(Lab);
  ShowMessage(BoolToStr(Assigned(Lab), true));
end;
like image 958
Wizzard Avatar asked May 03 '26 07:05

Wizzard


1 Answers

i think

Lab.Parent:= NIL;
FreeAndNil(Lab);

could help.

like image 116
punker76 Avatar answered May 06 '26 11:05

punker76