读书人

【】写的控件 如何设置属性默认值

发布时间: 2012-10-20 14:12:47 作者: rapoo

【求救】写的控件 怎么设置属性默认值?

我用的是delphi7 在控件定义的地方已经加了每个属性的default
published
{ Published declarations }
property Address :string read FsAddress write setAddress;
property Size :Integer read FiSize write SetSize default 56;
property TimeOut :Integer read FiTimeOut write SetTimeOut default 4000;
property TTL :Integer read FiTTL write SetTTL default 64;
property Times :Integer read FiTimes write SetTimes default 10;

但是把控件托到form上时这几个属性默认值都是0,不知道是怎么回事


[解决办法]
Delphi控件指定属性的时候可以加上Default关键字,例如property Color default clBtnface。一直以为这个是构造类的时候的默认值,所以当Color属性的默认值设定clBtnface,却发现实际构造出来的Color属性的值仍然是clDefault,觉得疑惑不解,后来才得知,这个default跟构造的默认值无关,而是跟dfm文件的存储有关,当color属性设定成clBtnface的时候,dfm文件就不存储这个属性的值,以节省存储控件,如果color的属性设置成clWhite,则保存文件后,Color的属性是存储到dfm文件中的。所以如果要想Color值有预设值,则需要乖乖地在构造函数中添加代码FColor := clBtnface;

你可以参考下上面。

另设置默认值的话还得在pas代码里写。

读书人网 >.NET

热点推荐