Forum > Beginners
[SOLVED] How to delete a button on an OnClick event
(1/1)
tudi_x:
Hi!
I am generating a plus and a minus button at run time on a form.
The behavior would be that the minus button would delete both buttons.
I am deleting the minus button with code below but I need an event different than the OnClick of the button I need to delete to hook it.
--- 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 TForm1.ManageMinusBtn(ATag : LONGINT; AState : BOOLEAN; ADelete : BOOLEAN = False);VAR i : INTEGER; child : TWinControl; BEGIN FOR i := 0 TO Form1.ControlCount - 1 DO BEGIN child := TWinControl(Form1.Controls[i]); IF child is TCDButton THEN IF child.Name = 'M' + IntToStr(ATag) THEN IF Assigned(child) THEN BEGIN IF ADelete THEN FreeAndNil(child) ELSE child.Enabled := AState; break; END; END;END;
Please advise what would be the best way to achieve a delete of the minus button on an OnClick user action.
Thank you
Thaddy:
I would not delete them. (Why? You can't get them back..) I would hide them. Button.Hide. And the correct way to delete is to call Button.Release. After that (either hide or release) you probably have to force a repaint with Form.invalidate.
Also, if I remember well if the Onclick is supposed to delete its own component you will have to use an intermediate variable of type TButton assign your button to it (self) and call release on that temp variable.
Bart:
--- Quote from: Thaddy on August 04, 2016, 05:13:14 pm ---Also, if I remember well if the Onclick is supposed to delete its own component you will have to use an intermediate variable of type TButton assign your button to it (self) and call release on that temp variable.
--- End quote ---
You can use Application.ReleaseComponent(TheButton) in this scenario, this way you won't destroy the button while it is in one of it's own eventhandlers.
Bart
Thaddy:
Tnx Bart,
I wasn't aware of that. I assume it does something similar like my Delphi scenario? And it is Lazarus only, I presume?
Bart:
I don't really know.
I once had a mysterious (that is: mysterious to me) bug when freeing a control on a PageControl, and some of the devels suggested this to me.
D7 does not have Application.ReleaseComponent, so probably it's a Lazarus invention.
Basically it will add the component to a list and then does QueueAsyncCall(@FreeComponent, 0), and FreeComponent calls the routine that releases the components in the list.
Bart
Navigation
[0] Message Index