tags:

views:

134

answers:

2

Hi,

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 Self.Text=''.

TAmountEdit = class (TEdit)
.
.
public
  procedure KeyUp(var Key: Word; Shift :TShiftState);
end;

procedure TAmountEdit.KeyUp(var Key: Word; Shift :TShiftState);
begin
inherited;
if Text='' then Free; // after calling free, an exception arises
end;

How should do to achieve the same effect?

Thanx

+4  A: 

Haven't tested it, but it should work:

Edit: Tested, works!
Edit 2: Now using CM_RELEASE instead of custom window message. Kudos to LachlanG!

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;

Your component should be freed when the message is handled either automatically or when you call Application.ProcessMessages.

Note: Yepp this is kind of a workaround and there might be better solutions out there ;)

kroimon
No need to declare a new message ID. Check out CM_RELEASE, which is designed specifically to do this for forms. But the basic idea is correct. +1
Mason Wheeler
YES. It works !!! Thanx a lot.... :-)
lyborko
So shouldn't somebody edit this answer to use the CM_RELEASE message ID instead. :-)I'd do it but I don't have enough reputation.
LachlanG
I would call this method a hack, because it is not 100% safe. Try use Release here: TForm2 = class(TForm) S:string; procedure WndProc(var Msg:TMessage);override; public constructor Create(AOwner:TComponent);override; //... constructor TForm2.Create(AOwner:TComponent); begin S:='abc'; inherited end; procedure TForm2.WndProc(var Msg:TMessage); begin inherited; S[2]:='x' {*} end;
Torbins
Edited it to use `CM_RELEASE` now :) Kudos to LachlanG!
kroimon
+3  A: 

Before implementing this I would stop and ask "Is this really the best approach?"

Do you really want an edit control class that always destroys itself when key input results in the Text property becoming an empty string?

Is it not more likely to be the case that you have a specific form/dialog where this behaviour is required? In which case, there is no problem... you can free the edit control in the KeyUp event handled by the form without incurring an Access Violation.

Deltics
Yes, I know it. The problem is, that I have one (my own) component based on TCustomControl, which contains TAmountEdit Controls (not specified how many). Thats why I have to dynamicaly create them and later Free them according my rules... It is similar like tabs on the IE or FireFox, where I close the tab and it disappears... (Frees? I do not know... :-) )
lyborko
I see. But if your component creates the controls, then to my mind it should also be responsible for freeing them. The rules would seem to be set by the control *containing* the **TAmountEdit** controls, not by the **TAmountEdit** control itself. Your custom control would create a **TAmountEdit** then assign an event handler (that it implements) to the **OnKeyUp()** event of the **TAmountEdit** (note that technically your original question is not about handling an event but rather *intercepting* one!).
Deltics
:-) I do not concern about freeing TAmountEdit, while they are added to Components array of the containercontrol (it owns them). So they are freed automaticaly, when containercontrol is about to be destroyed. I intercept OnKeyUp() event of TAmountEdit anyway in order to navigate through them easily. To free TAmountEdit is its optional property and I wanted to implement it inside the control itself. Moreover I had some specific difficulties to free control from the parent just because of intercepting OnKeyUp event...
lyborko
It is far less problematic and less risky for a parent to free a child than it is for any object to free itself. But it's your funeral. :)
Deltics