关注

解决方案 »

  1.   

    在构造函数里初始化,直接赋值。
    同时配合default 'abc'一起使用.
      

  2.   

    string 类型的属性根本不允许 Defautl 来赋值
      

  3.   

    只能在构造函数中给String赋默认值。Default是不能给数据赋默认值的。
      

  4.   

    delphi里不知道是怎么处理的,在vc++中,首先添加一个属性,如:caption,然后还要定义一个用来存储属性值的成员变量,属性的值就取决于该成员变量的值。
    所以赋值并不是给caption,而是给哪个成员变量,caption靠setcaption和getcaption来对其值进行操作。
      

  5.   

    TYourComponent = class(..)
    private
      Fs : string;
    public
      construtor create;TYourComponent.create;
    begin
      Inherited;
      Fs := 'abc';
    end;
      

  6.   

    楼主,Default指定的根本不是默认值,而是指定写入DFM文件
    默认值是在构造函数中写的。
      

  7.   

    The default directive must be followed by a constant of the same type as the property. For example,property Tag: Longint read FTag write FTag default 0;To override an inherited default value without specifying a new one, use the nodefault directive. The default and nodefault directives are supported only for ordinal types and for set types, provided the upper and lower bounds of the set抯 base type have ordinal values between 0 and 31;To: linzhengqun(风)
    default 另一个功能就设置默认值。但是正如帮助里说的,只支持有序类型的。
    所以string类型不能设置默认值。也就是说应该在 create 中设置初值。
      

  8.   

    to:jacky_shen(jacky) 以下摘自帮助:
    Property values are not automatically initialized to the default value. That is,
    属性值并不自动初始化默认值,也就是说,default指示符仅仅只是控制属性值什么时候保存
     the default directive controls only when property values are saved to the form 
    到窗体文件中(即DFM),而不是为一个新创建的实例初始化一个属性值
    file, but not the initial value of the property on a newly created instance.我不知道你有没有实践过,实事上是不能设置默认值的,我举个简单的例子给你看:
    unit MyEdit;interfaceuses
      Windows, Messages, Classes, Forms,Controls, Graphics, StdCtrls;type
      TMyEdit = class(TCustomEdit)
      private
        { Private declarations }
        FCount:integer;
      protected
        { Protected declarations }
      public
        { Public declarations }
         constructor Create(AOwner:TComponent);override;
      published
        { Published declarations }
        property count:integer read Fcount write Fcount default 5;
      end;
    procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Wind', [TMyEdit]);
    end;constructor TMyEdit.Create(AOwner:TComponent);
    begin
       inherited Create(AOwner);
    end;
    end. 
    把这个控件安装之后,在对象察看器中,看到的Count的值是0,而不是5
    如果你在构造函数中加上这一句:
    Fcount:=2;
    则对象察看器中的Count值是2
    而你在察看器设Count等5,再去DFM文件中看一下,这个属性没有列出来
    而如果设为其他值,在DFM文件中则会列出这个属性值来。以上是我的实践的结果,不知道你所说的默认值是什么,可能是我还不知道
    你说的另一个功能要怎么实现,请指教
      

  9.   

    To: linzhengqun(风)
    不要意思,帮助没有看仔细。其实 default 的作用一个是设置数组属性的为类的默认属性,另一个就是将属性值与 default 的值进行比较,如果有改动或者 stored 为 True,那该值就会被保存下来。
      

  10.   

    为什么?因为组件的CREATE在调用序列里是非常靠前的,你在里面刚 FS:=‘ABC’,此后DELPHI的IDE会从DFM里继续读取此属性值(运行时从资源里读),读到的新的值就把‘ABC’覆盖了。
    保留CREATE里的代码,再继承一个LOADED方法可解决此问题。
    大概这样
    PROCEDURE LOADED;
    BEGIN
      IF LENGTH(FS)=0 THEN FS:=‘ABC’;
    END;
      

  11.   

    DEFAULT关键字不可以用于STRING属性?行的话就没这些麻烦了,记不住,得看看HELP了
      

  12.   

    我测了一下,不用覆盖LOAD方法,CREATE里就够了。你的如果不好使,除非使用了NODEFAULT修饰符。