How to Create and Destroy TGrid at Runtime in Firemonkey — Android and iOS App Dev

  

I have a TGrid created at runtime. The procedure requires that I should destroy TGrid before I can re-create at "add item and refresh" button click. I noticed that if I don’t destroy TGrid before re-creating it, heavy overheads cause to freeze my App on over 8 or more times of doing it.
I tried the following codes but no avail:
procedure TformMain.AddItemRefreshClick(Sender: TObject);
var
TGrid1 : TTGrid;
begin
if Assigned(TGrid1) then
begin
TGrid1.DisposeOf;
{TGrid1.Free; Tried this also but not working}
{TGrid1 := nil; Tried this also but not working}
end;

TGrid1 := TTGrid.Create(formMain);
With TGrid1 do
begin
Parent := formMain;
Align := TAlignLayout.Client;
Margins.Top := 5;
Margins.Left := 5;
Margins.Right := 5;
Margins.Bottom := 5;
ScrollBars.Visible := True;
Header.Format.Font.Size := 11;
Cells.Format.Font.Size := 11;
TabOrder := 0;
end;
end;

I am getting Access Violation at Address… Sounds heave error!
Is there a simpler way that I can create and destroy visual component like TGrid at runtime?

Comments are closed.