如何在其事件处理程序中自由控制? [英] How to free control inside its event handler?
问题描述
我想要自由动力创建TEdit,当Self.Text =''。
TAmountEdit = class(TEdit)
。
。
public
程序KeyUp(var Key:Word; Shift:TShiftState);
结束
程序TAmountEdit.KeyUp(var Key:Word; Shift:TShiftState);
开始
继承;
如果Text =''then Free; //在调用free之后出现异常
end;
如何达到同样的效果?
Thanx
没有测试过,但它应该工作:
编辑:测试,工作!
编辑2:现在使用 CM_RELEASE
而不是自定义窗口消息。对于LachlanG!的批评!
界面
类型
TAmountEdit =类(TEdit)
..
程序KeyUp(var Key:Word; Shift:TShiftState);覆盖
procedure HandleRelease(var Msg:TMessage);消息CM_RELEASE;
...
end;
实现
程序TAmountEdit.KeyUp(var Key:Word; Shift:TShiftState);
开始
继承;
如果Text =''then
PostMessage(Handle,CM_RELEASE,0,0);
结束
程序TAmountEdit.HandleRelease(var Msg:TMessage);
开始
免费;
结束
当消息自动处理或当您调用 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=''. How should do to achieve the same effect? Thanx Haven't tested it, but it should work: Edit: Tested, works! Your component should be freed when the message is handled either automatically or when you call Note: Yepp this is kind of a workaround and there might be better solutions out there ;) 这篇关于如何在其事件处理程序中自由控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
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;
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;
Application.ProcessMessages
.