How to free control inside its event handler?

前端 未结 2 455
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 09:23

Does anybody know the trick, how to free control inside its event handler ? According delphi help it is not possible...

I want to free dynamicaly created TEdit, when

2条回答
  •  醉话见心
    2020-11-30 09:51

    The solution is to post a queued message to the control, which it responds to by destroying itself. Ny convention we use CM_RELEASE which is the private message used by TForm in its implementation of the Release method that performs an analogous task.

    interface
    
    type
      TAmountEdit = class (TEdit)
        ...
        procedure KeyUp(var Key: Word; Shift :TShiftState); override;
        procedure HandleRelease(var Msg: TMessage); message CM_RELEASE;
        ...
      end;
    
    implementation
    
    procedure TAmountEdit.KeyUp(var Key: Word; Shift :TShiftState);
    begin
      inherited;
      if Text = '' then
        PostMessage(Handle, CM_RELEASE, 0, 0);
    end;
    
    procedure TAmountEdit.HandleRelease(var Msg: TMessage);
    begin
      Free;
    end;
    

    The control is destroyed when the application next pumps its message queue.

提交回复
热议问题