如何在其事件处理程序中自由控制? [英] How to free control inside its event handler?

查看:103
本文介绍了如何在其事件处理程序中自由控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道这个窍门,如何在事件处理程序中自由控制?根据delphi帮助它是不可能的...



我想要自由动力创建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=''.

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

解决方案

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 ;)

这篇关于如何在其事件处理程序中自由控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆