2010. január 17., vasárnap

Delete a TFrame together with its parent TTabSheet


Problem/Question/Abstract:

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.

const
  UM_DESTROYCONTROL = WM_USER + 230;

  {in form declaration}
  private
    { Private declarations }

procedure UmDestroyControl(var msg: TMessage); message UM_DESTROYCONTROL;

{in the buttons OnClick handler}

var
  ctrl: TWinControl;
begin
  ctrl := GetParentForm(Sender as TButton);
  PostMessage(ctrl.handle, UM_DESTROYCONTROL, 0, Integer(Sender));
  { ... }

procedure TForm1.UmDestroyControl(var msg: TMessage);
begin
  TObject(msg.lparam).Free;
end;

Nincsenek megjegyzések:

Megjegyzés küldése