请教关于组件开发的基本问题(100)

D

delhpi

Unregistered / Unconfirmed
GUEST, unregistred user!
第一次学习写组件,写了个最简单的.组件功能目的:把组件拖到主FORM后,VerDate属性值自动为当天日期,这样我在程序里可以读取VerDate的值,用来组合成字符串,比如XXX软件 2009-07-31,作为软件标题.以后每次打开工程编译时,VerDate都能自动为当天日期.这样我每次修改工程,编译时就不用手工在代码里修改那个日期值了.也就是什么日期修改编译了,以后软件运行时,软件标题自动显示XXX软件+最后编译的日期。虽然自己把功能实现了,组件也安装到了IDE,但是自己还是感觉很糊涂。按照我的想法,认为只要在构造器函数里像下面这样写就行了。constructor TVerDateEdit.Create(AOwner: TComponent);begin inherited Create(AOwner); if csDesigning in ComponentState then FVerDate:=FormatDateTime('yyyy-mm-dd',date);//这行代码,每次打开工程时应该都运行啊end;但现在好像只能增加一个属性FAutoDate: boolean;,然后在写方法里再增加代码才能实现效果。procedure TVerDateEdit.SetAutoDate(Value: Boolean);begin FAutoDate:=value; if FAutoDate and (csDesigning in ComponentState) then//运行状态时,不用修改 FVerDate:=FormatDateTime('yyyy-mm-dd',date);//以后每次打开工程时,设置当天日期end;下面是完整的组件代码unit VerDateEdit;interfaceuses SysUtils, Classes, Controls, StdCtrls;type TVerDateEdit = class(TComponent) private { Private declarations } FVerDate:String; FAutoDate: boolean; procedure SetAutoDate(Value:Boolean); protected { Protected declarations } public { Public declarations } constructor Create (AOwner:TComponent);override; published { Published declarations } property VerDate :string read FVerDate Write FVerDate; property AutoDate:boolean read FAutoDate Write SetAutoDate; end;procedure Register;implementationprocedure Register;begin RegisterComponents('Standard', [TVerDateEdit]);end;{ TVerDateEdit }constructor TVerDateEdit.Create(AOwner: TComponent);begin inherited Create(AOwner); FVerDate:=FormatDateTime('yyyy-mm-dd',date);//组件拖到FORM时,设置当天日期 FAutoDate:=true;end;procedure TVerDateEdit.SetAutoDate(Value: Boolean);begin FAutoDate:=value; if FAutoDate and (csDesigning in ComponentState) then//运行状态时,不用修改 FVerDate:=FormatDateTime('yyyy-mm-dd',date);//以后每次打开工程时,设置当天日期end;end.
 
可惜 没有人愿意指点一下?
 
每次打开窗体时,会从窗体文件中读取上次保存的属性值,而这个过程在构造方法之后执行,所以你在构造方法中写的那句话的效果,就被替换掉了。可以把FVerDate:=FormatDateTime('yyyy-mm-dd',date) 这句话放在 loaded 方法中。
 
谢谢楼上,有学习了一下,有点明白了。loaded过程在IDE从DFM中读取完组件属性后运行。修改版如下unit VerDateEdit;interfaceuses SysUtils, Classes, Controls, StdCtrls;type TVerDateEdit = class(TComponent) private { Private declarations } FVerDate:String; protected { Protected declarations } procedure Loaded ;override; public { Public declarations } constructor Create (AOwner:TComponent);override; published { Published declarations } property VerDate :string read FVerDate Write FVerDate; end;procedure Register;implementationprocedure Register;begin RegisterComponents('Samples', [TVerDateEdit]);end;{ TVerDateEdit }constructor TVerDateEdit.Create(AOwner: TComponent);begin inherited Create(AOwner); if csDesigning in ComponentState then FVerDate:=FormatDateTime('yyyy-mm-dd',date);//第一次拖到FORM上会执行,以后每次打开工程也会执行end;procedure TVerDateEdit.Loaded;begin inherited; if csDesigning in ComponentState then FVerDate:=FormatDateTime('yyyy-mm-dd',date);//第一次拖到FORM上不会执行,以后每次打开工程时执行end;end.
 
第一次写组件就遇到个不简单的问题.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
920
DelphiTeacher的专栏
D
I
回复
0
查看
706
import
I
顶部