如何在自定义控件中定义新的事件?(100分)

  • 主题发起人 frankwood
  • 开始时间
F

frankwood

Unregistered / Unconfirmed
GUEST, unregistred user!
如何在自定义控件中定义新的事件?
我的信箱是frank_w@21cn.com,如果谁有例子代码,可直接发到我的信箱。多谢各位
 
type Tmyedit =class(Tedit);
procedure ontextis_hello;

Tmyedit.onChange
begin
if text='hello' then
if ontextis_helo<> nil then
ontextis_helo

end;
 
我也见过一种定义,不知道两种方法有什么不同,能详细的告诉我定义事件的语法结构吗?
我见过的定义如下:
...
TOnReceive = procedure(Sender: TComponent; NumberBytes: Integer; FromIP: string; Port: integer) of object;
TtestUDP = class(TComponent)
private
FOnDataReceived: TOnReceive;
...
 
《delphi5开发人员指南》中的编写自定义控件。那里有很好的例子。
 
to frankwood:

TOnReceive = procedure(Sender: TComponent; NumberBytes: Integer; FromIP: string; Port: integer) of object;
TtestUDP = class(TComponent)
private
FOnDataReceived: TOnReceive;
上面代码中,TOnReceive是一种数据类型,确切的说是指针类型的,FOnDataReceived当然
就是指针变量了,它将指向一段代码(过程)的开始地址,而且这过程必须是TOnReceive 定义那样。
指针可能是空的,此时说明还没有为该事件写代码——该事件不存在。
当你为某个事件写代码时,开发工具替你做了两件事:
1、产生一个空的过程,让你在其中写代码;
2、将这个过程的地址给事件指针TOnReceive ,此时指针不是空的了。

一般,触发该事件时会判断一下:
if FOnDataReceived<>nil then
FOnDataReceived(...);
 
谢谢大家
 
顶部