I need to delete a component by clicking on it. I think I have found the solution but I would like someone to confirm that it is correct to avoid anomalous behavior of the application.
I have a
TScrollBox in which the user can create
TPanels at runtime which can contain other different components.
In each of them a
TSpeedButton is created which allows to eliminate the
TPanel with all its contents.
This is a part of the code when creating the
TPanel NewBtn := TSpeedButton.Create(NewPanel);
NewBtn.Parent:=NewPanel;
NewBtn.Align:=AlLeft;
NewBtn.Images:=ImageList1;
NewBtn.ImageIndex:=4;
NewBtn.BorderSpacing.Left:=2;
NewBtn.BorderSpacing.Right:=2;
newbtn.Name:='btn'+dt;
newbtn.OnClick:=@RemovePanel;
and this is the onclick event of the
TSpeedButtonvar
Mypanel: TComponent;
Str, Search: string;
Ssender: TSpeedButton;
begin
with Sender as TSpeedButton do
begin
Ssender := Sender as TSpeedButton;
Str:=StringReplace(Ssender.Name, 'btn', '', [rfIgnoreCase, rfReplaceAll]);
Search := 'Panel'+Str;
Mypanel := Scrollbox1.FindComponent(Search);
FreeAndNil(Mypanel);
// Application.ReleaseComponent(Ssender);
end;
end;
If after executing this code I search with
FindComponent for the
TSpeedButton it is still assigned even though it is no longer visible.
If I uncomment the
ReleaseComponent the
TSpeedButton seems to no longer exist.
All components created in the panel have the panel itself as owner and parent, so deleting the panel should also delete all its children.
But the
ReleaseComponent is still in the event of the
TSpeedButton, so is it correct to delete it this way?