当前位置:编程学习 > Delphi >>

delphi重写事件

我现在用到一个自动搜索sql数据库服务器的组件,然后前台的程序当执行完搜索功能以后需要弹出一个框说明一共发现多少服务器。于是我找到组件里的搜索完成事件RefreshComplete,在这个事件里多加了一句代码,
loginFrm.tray1.ShowBalloonHint('搜索完成','共发现 '+inttostr(self.Items.Count)+' 台数据库服务器! ',bitInfo,10);

可是这样以后再次使用这个组件的时候,这句代码就又得重新在这个pas改过来.
所以我想这样实现功能,在RefreshComplete事件里加入一个函数接口,内容为空,然后在其他窗体类需要使用这个组件的时候,重写这个函数接口,实现搜索完成的提示功能。如果说不清楚,请留下qq,给我远程一下,谢谢
答案:Event实际上就是一个Property,只不过它的数据类型比较特殊,是一个函数指针。
看看这个组件的源代码里哪里引用了这个事件的所代表的函数指针。一般是在某个消息的处理过程里面,有这样的代码
if Assigned(FRefreshComplete) then FRefreshComplete(...)
找到了这个过程就好办了。在本类中把它设置成虚方法。

重新建一个新的组件,以“自动搜索sql数据库服务器的组件”为基类。然后对前面说的这个过程进行重载就可以了。先继承基类的方法,然后再加上你的代码。如你的:
loginFrm.tray1.ShowBalloonHint('搜索完成','共发现 '+inttostr(self.Items.Count)+' 台数据库服务器! ',bitInfo,10);

以后你就用你自己建的这个新的组件就可以了。

这就是面向对象的威力所在啊。继承,重载!

如果说的不够明白的话,我可以做个例子给你看。

unit Unit2;

interface

uses Windows, Messages, Classes;

type
TAncestor = class(TComponent)
private

FOnRefreshComplete: TNotifyEvent;

procedure SetOnRefreshComplete(const Value: TNotifyEvent);
protected

procedure DoRefreshComplete;virtual;
published

property OnRefreshComplete: TNotifyEvent read FOnRefreshComplete write SetOnRefreshComplete;
end;

TOffSpring = class(TAncestor)
protected

procedure DoRefreshComplete;override;
published

property OnRefreshComplete;
end;

implementation

{ TAncestor }

procedure TAncestor.DoRefreshComplete;
begin
if Assigned(FOnRefreshComplete) then

FOnRefreshComplete(Self);
end;

procedure TAncestor.SetOnRefreshComplete(const Value: TNotifyEvent);
begin
FOnRefreshComplete := Value;
end;

{ TOffSpring }

procedure TOffSpring.DoRefreshComplete;
begin
inherited;
ShowMessage('put my own codes here');
end;

end.

上一个:为什么我一运行编程软件就弹出这个东西?
下一个:Delphi问题

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,