I’m working on a component that is placed on every form of my project. Is it possible, in runtime, to have the component include code into it’s owner form’s OnClose event handler. In other words, the form will trigger it’s own OnClose event handler but the component will also include additional event handler code to run on the owner form’s OnClose event. (Is that what is called vector replacement?)
Thank you.
I’m working on a component that is placed on every form of my project.
Share
You need to get the component to declare a field to store the form’s original
OnClose. Then you can do in the component’s constructor:Then the component’s
FormClosewould read:Naturally the
ascast ties this component to being owned by forms but if you want more flexibility you could easily cater for that.This is a direct answer to the question that you asked, but it would be remiss of me not to question your overall design. If you want a component to live on every form in your app then surely you should derive a subclass of
TFormthat contains your customisations. Then make every form in your app be based on that common base form class.That approach has many other benefits. For example, @LachlanG adds the following very apt comment with which I wholeheartedly concur:
The common base form approach solves this by placing the code that works with the form inside the form.
If you are do go down the route of having a common base form then you should override
DoCloserather than using theOnCloseevent. Always use theDoXXXevent raisers rather than the events themselves when you are creating a common base class or a component.