在自定控件中怎样给On事件赋值???(200分)

  • 主题发起人 主题发起人 ZZHI
  • 开始时间 开始时间
Z

ZZHI

Unregistered / Unconfirmed
GUEST, unregistred user!
TServerSocket = class(TCustomServerSocket)中
的Create事件中怎样给OnClientRead赋值??

我写成
OnClientRead:=MyClientRead;老报错!
在TServerSocket 中加有
procedure MyClientRead(Sender: TObject; Socket: TCustomWinSocket);

望知道原因的大富翁能指点一二,谢谢!!!
 
不会呀,我的就没错
前面加上Self 试试
Self.OnClientRead:=MyClientRead;

你的出错信息?
 
要在自定义控件中的Create事件中同!!!
不好意思,忘了讲是运行时出错!
 
没错,我就是在Create事件中设定的,运行一切OK,能不能把你的问题讲的详细点
 
OnClientRead这个指针所指向的过程必须属于某个类,而不能是个
普通的全局过程。
 
有誰能成功请发个可运行的给我,z_zhi@cmmail.com,谢谢!
 
你的MyClientRead是不是没有定义在一个对象上面的纯procedure?如果是这样你大概需要定义一个只有这个方法的对象,然后自己create和free这个对象,然后赋值试试看
 
To Xeen:
普通的全局过程是什么意思?,属于窗体类也是可以的呀,
不过Xeen提醒了我,我说主楼主你是不是把你那个过程定义在窗体类里啦,那样的话
Self.OnClientRead:=Form1.MyClientRead;
肯定OK了
不过你最好把它定义在你那个类的Protected里比较好


 
to beyondair:既然是运行时错误,肯定不是这个原因了,要不编译
通不过。
 
希望大家试试,很简短的代码。
为什么会出错,我实在查不出原因,望大家帮帮忙!!!
 
type
TmyServerSocket = class(TCustomServerSocket)
private
FServerSocket: TServerWinSocket;
public
constructor Create(AOwner: TComponent); override;
property Socket: TServerWinSocket read FServerSocket;
procedure MyClientRead(Sender: TObject; Socket: TCustomWinSocket);
...
end;

constructor TmyServerSocket.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FServerSocket := TServerWinSocket.Create(INVALID_SOCKET);
InitSocket(FServerSocket);
FServerSocket.ThreadCacheSize := 10;
OnClientRead:=MyClientRead; //加上这句出错
end;



 
把 TmyServerSocket = class(TCustomServerSocket)
改成
TmyServerSocket = class(TServerSocket)

这该OK了吧
 
见DELPHI帮助

Do not create instances of TCustomServerSocket. Use TCustomServerSocket as a base class when declaring
Server socket objects that listen for connection requests
from other machines.
 
要从TCustomServerSocket继承的话,要怎么解决呢?为什么会出错?
 
见 unit ScktComp;的类声明和方法的实现

TServerSocket = class(TCustomServerSocket)
public
constructor Create(AOwner: TComponent); override;
property Socket: TServerWinSocket read FServerSocket;
published
property Active;
property Port;
property Service;
property ServerType;
property ThreadCacheSize default 10;
property OnListen;
property OnAccept;
property OnGetThread;
property OnGetSocket;
property OnThreadStart;
property OnThreadEnd;
property OnClientConnect;
property OnClientDisconnect;
property OnClientRead;
property OnClientWrite;
property OnClientError;
end;


TCustomServerSocket = class(TCustomSocket)
protected
FServerSocket: TServerWinSocket;
procedure DoActivate(Value: Boolean); override;
function GetServerType: TServerType;
function GetGetThreadEvent: TGetThreadEvent;
function GetGetSocketEvent: TGetSocketEvent;
function GetThreadCacheSize: Integer;
function GetOnThreadStart: TThreadNotifyEvent;
function GetOnThreadEnd: TThreadNotifyEvent;
function GetOnClientEvent(Index: Integer): TSocketNotifyEvent;
function GetOnClientError: TSocketErrorEvent;
procedure SetServerType(Value: TServerType);
procedure SetGetThreadEvent(Value: TGetThreadEvent);
procedure SetGetSocketEvent(Value: TGetSocketEvent);
procedure SetThreadCacheSize(Value: Integer);
procedure SetOnThreadStart(Value: TThreadNotifyEvent);
procedure SetOnThreadEnd(Value: TThreadNotifyEvent);
procedure SetOnClientEvent(Index: Integer; Value: TSocketNotifyEvent);
procedure SetOnClientError(Value: TSocketErrorEvent);
property ServerType: TServerType read GetServerType write SetServerType;
property ThreadCacheSize: Integer read GetThreadCacheSize
write SetThreadCacheSize;
property OnGetThread: TGetThreadEvent read GetGetThreadEvent
write SetGetThreadEvent;
property OnGetSocket: TGetSocketEvent read GetGetSocketEvent
write SetGetSocketEvent;
property OnThreadStart: TThreadNotifyEvent read GetOnThreadStart
write SetOnThreadStart;
property OnThreadEnd: TThreadNotifyEvent read GetOnThreadEnd
write SetOnThreadEnd;
property OnClientConnect: TSocketNotifyEvent index 2 read GetOnClientEvent
write SetOnClientEvent;
property OnClientDisconnect: TSocketNotifyEvent index 3 read GetOnClientEvent
write SetOnClientEvent;
property OnClientRead: TSocketNotifyEvent index 0 read GetOnClientEvent
write SetOnClientEvent;
property OnClientWrite: TSocketNotifyEvent index 1 read GetOnClientEvent
write SetOnClientEvent;
property OnClientError: TSocketErrorEvent read GetOnClientError write SetOnClientError;
public
destructor Destroy; override;
end;
 
主要区别在于TCustomServerSocket和TServerSocket对于OnClinetRead事件的声明不同
在你的原程序中加一句

type
TmyServerSocket = class(TCustomServerSocket)
private
FServerSocket: TServerWinSocket;
public
constructor Create(AOwner: TComponent); override;
property Socket: TServerWinSocket read FServerSocket;
procedure MyClientRead(Sender: TObject; Socket: TCustomWinSocket);
...
published // 加上这部分声明**********************
property OnClientRead;
end;

constructor TmyServerSocket.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FServerSocket := TServerWinSocket.Create(INVALID_SOCKET);
InitSocket(FServerSocket);
FServerSocket.ThreadCacheSize := 10;
OnClientRead:=MyClientRead;//OK!
end;
 
多人接受答案了。
 
我因为不想开放OnClientRead才没有写在published中,看来只有换方法了。
不过还是得谢谢beyondair!
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
543
import
I
I
回复
0
查看
627
import
I
后退
顶部