Forum > General

Use of ReleaseComponent

(1/2) > >>

valter.home:
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


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  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 TSpeedButton


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  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?

OwlOfTime:
Have you tried it?

Remember that assigned will return true if the variable is not set to nil like when using freeandnil.

What I do is with a notify event pass the deletion of the control to the parent container, the container that will not be removed. And I just free the control myself so no need to use release component.

valter.home:

--- Quote ---Have you tried it?
--- End quote ---

Yes, it would seem to work but I was wondering if it couldn't create random problems.

I used this code for verification


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TMainForm.Button1Click(Sender: TObject);var  SearchButton: TComponent;  BtnName: string;begin  BtnName := Edit1.Text;   SearchButton := FindComponent(BtnName);  if assigned(SearchButton) then    memo1.lines.add('The button exists')  else     memo1.lines.add('The button does not exist'); end;
Since if I delete the panel that contains the TSpeedButton I no longer know how to access it to verify its existence, I changed this line in the creation of the panel

from

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---NewBtn := TSpeedButton.Create(NewPanel);to

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---NewBtn := TSpeedButton.Create(self);
Now when the panel exists the above code confirms the existence of the TSpeedButton, when I delete the panel also the TSpeedButton is no longer found.

Do you think this is not the right way?


lucamar:
If the panel is the Owner of the button you don'r need to use ReleaseComponent. That's what being Owner means: the panel is responsible for the lifetime of its owned components; free it and it will in turn free them.

valter.home:
Thank you,
so if I have a MyPanel panel with a TButton created like this


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---NewBtn := TButton.Create(MyPanel);NewBtn.Parent:=MyPanel;NewBtn.OnClick:=@RemovePanel;
if I use in the RemovePanel procedure


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---FreeAndNil(MyPanel);
will also remove the TButton even though we are still in its event?
Actually, if I search for it then I can't find it but it seemed to me that I saw around that it was not correct to remove a Parent using an event of one of his children just because the child depends on the parent.

Thanks so much


Navigation

[0] Message Index

[#] Next page

Go to full version