If you go through the code and check carefully the event handler is ButtonReplaceClick, and the button being destroyed is Button1, not the one being clicked.
Therefore: ButtonReplace is the button being clicked, and it's the one whose OnClick is being handled.
Button1 is just another button on the form, and it’s being destroyed inside ReplaceButtonWithProgressBar, but not in its own event.
Sorry, I really didn't notice that these are different buttons. As I said, I haven't tested the code, and just wanted to share my knowledge. Of course, to avoid misunderstandings, I have edited the comment, and I hope that will be forgiven ))
There is no need to apologize you were trying to help. You just had not tested the code yet. It is just that folks tend to forget we were all *newbies* at one point in our lives. And I am now weary of seeing people ask questions and not get useful nor helpful answers and guidance but a 'That is not how you do things' or 'This is bad code' or an answer with information that is way above and beyond a new comers level of knowledge or familiarity with the IDE which in turn causes frustration and most folks will never want to come back?
Also, No offence and no disrespect but the knowledge you shared below:
* Will this code restore the Z-order of the component? The order of the controls may affect the display, if they overlap or use ChildSizing.
* Will it restore the TabOrder value? Because of this, the [Tab] key navigation may go in a different order.
* Will it work when using Align? Different controls may be reordered when the direction is the same.
* Will it work when using ChildSizing? By the way, it also depends on the Z-order.
* Other huge number of nuances AutoSize, Constraints, BorderSpacing...
How many new comers will really understand what it is your trying to tell them? This will not help a newbie this only demoralizes, demotivates and we lose a developer. Not good. When someone has *your* kind of experience and expertise yes that is a solid useful helpful response but in a newbies situation that will only help them to leave and fast.
This reminds me I recently had trouble with Z-order and I am using Linux Debian with Lazarus 4 and I was trying to move a Timage to the front of a TMemo but was unable to do so. Later investigations with other components gave the same result Z-order is not honored or functional in design mode. I am not sure if I should open a new thread or investigate this further?