Author: Tomas Rutkauskas
I have a form with a page control, and each tab sheet of the page control contains
a frame. I would like to delete the frame along with the parent tab sheet if the
user clicks a certain button on the frame. What's the best way to do this? It seems
that if the tab sheet's free method is called from inside the button-click event
handler, the button will be freed before the event handler is finished executing.
Answer:
The way to solve this is do like TCustomform.Release does it: post (via
PostMessage) a user message to the form, have the form free the component in
response to the message.
1 const2 UM_DESTROYCONTROL = WM_USER + 230;
3 4 {in form declaration}5 private6 { Private declarations }7 8 procedure UmDestroyControl(var msg: TMessage); message UM_DESTROYCONTROL;
9 10 {in the buttons OnClick handler}11 12 var13 ctrl: TWinControl;
14 begin15 ctrl := GetParentForm(Sender as TButton);
16 PostMessage(ctrl.handle, UM_DESTROYCONTROL, 0, Integer(Sender));
17 { ... }18 19 procedure TForm1.UmDestroyControl(var msg: TMessage);
20 begin21 TObject(msg.lparam).Free;
22 end;