如何为我的控件加CLICK事件?(50分)

  • 主题发起人 czdomybest
  • 开始时间
C

czdomybest

Unregistered / Unconfirmed
GUEST, unregistred user!
我做了一个控件(TUltraReport)从TCustomContro继承而来,我想为它加一个CLICK事件,
我做了以下工作:
private
FOnClick:TNotifyEvent;
protected
procedure Click;dynamic;
published
property OnClick:TNotifyEvent read FOnClick write FOnClick;

procedure TUltraReport.Click;
begin
if Assigned(FOnClick) then FOnClick(self);
end;

在delphi编辑环境中,能看到该控件有了ONCLICK事件,我为这个事件写了代码,但我在程序
运行期,该事件却不能触发,请问该如何办?
 
unit UltraReport;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TUltraReport = class(TCustomControl)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
property OnClick ;//该事件在其父类TCustomControl—>TWinControl->TControl中定义过,在此你只需公布OnClick事件即可。
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TUltraReport]);
end;

end.
 
我照你说的做了,可它在运行时却不触发,请问是为什么?
 
还有一点疑问是:我将该控件放到窗口中,用鼠标怎么也选不中该控件,只能在Object Inspetor
的对象列表中才能将其选中,是不是这个原因致使CLICK事件不能触发。如果是,该如何解决?
 
这一段也要去掉:
procedure TUltraReport.Click;
begin
if Assigned(FOnClick) then FOnClick(self);
end;
 
设置Height,width属性,
 
unit UltraReport;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TUltraReport = class(TCustomControl)
private
{ Private declarations }
protected
{ Protected declarations }
public
constructor Create(AOwner: TComponent); override;
published
property Color;
property OnClick ;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('Samples', [TUltraReport]);
end;

{ TUltraReport }

constructor TUltraReport.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
Width:=100;
Height:=100;
Color:=clred;
end;

end.
 
我找到发生问题的地方了:
我的TUltraReport控件有两个属性:
FCurPage:TUltraPage;//当前页
FEditPage:TUltraPage;//编辑页
在TUltraPage的create事件里有这么两句
Width:=100;
Height:=100;
如果加这两句,ONCLICK就不能触发,如果不加,就可以触发,请问这是何故?
 
>>如果加这两句,ONCLICK就不能触发

不会吧。设置Width, Height应该跟ONCLICK毫不相关的。
要不你把你的代码贴出来或发给我看一下,如何?

my Email: llp166@263.net
 
程序如下:
unit ReportTest;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TReportTest = class;
TPageTest = class;

TReportTest = class(TCustomControl)
private
{ Private declarations }
FEditPage:TPageTest;
protected
{ Protected declarations }
//function GetValidRect:TRect;
procedure SetParent(AParent:TWinControl); override;
public
constructor Create(AOwner:TComponent);override;
destructor Destroy; override;
property EditPage:TPagetest read FEditPage ;//编辑页
published
{ Published declarations }
end;

TPageTest = class(TCustomControl)
private
FOwnerReport:TReportTest ;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
property OwnerReport:TReportTest read FOwnerReport;
end;

procedure Register;

implementation

procedure Register;
begin
RegisterComponents('程智组件', [TReportTest]);
end;

constructor TReportTest.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FEditPage:=Tpagetest.Create(self);
end;

destructor Treporttest.Destroy;
begin
inherited;
if not( csDestroying in EditPage.ComponentState ) then //如果EDITPAGE将要销毁
begin
EditPage.Free;
end;
end;

procedure TReporttest.SetParent(AParent:TWinControl);
begin
inherited;
if not (csDestroying in EditPage.ComponentState) then begin
EditPage.Parent:=Parent;
if Parent<>nil then begin
EditPage.Left :=10;
EditPage.Top:=10;
end;
end;
end;

constructor TPagetest.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
if AOwner is TReportTest then
FOwnerReport:=TReportTest(AOwner);
Color :=RGB(255,255,255);//背景色。
Font.Name:='宋体';
Font.Size:=9;
if OwnerReport<>nil then begin
Width:=100;//OwnerReport.ValidRect.Right-OwnerReport.Left ;
Height:=100;//OwnerReport.ValidRect.Bottom-OwnerReport.Top;
end;
end;

destructor TPagetest.Destroy;
begin
inherited Destroy;
end;

end.


问题出在TReporttest.SetParent上,但是我如果不用setparent,报表就不会显示了。
但用了它,onclick事件就不能用了。请问该如何解决。
 
czdomybest,你在什么地方公布了OnClick事件,我怎么没看见?
 
我没有加上去,加上去后也是不行的。
现在的问题是,我将该控件放到窗口中,用鼠标怎么也选不中该控件,只能在Object
Inspetor的对象列表中才能将其选中,是它导致了CLICK事件不能触发。如果不用
setparent,不光能选中该控件,ONCLICK事件也能够触发。
 
谢谢大家了!!!
 
顶部