如果性属存在一个变量中,Delphi如何设置这个变量啊,如:
var s:='Caption';怎么通过s设置一个From的Caption属性? 如:
form1.s:='标题';

解决方案 »

  1.   

    use Typinfo;var s :string;
    s :='Caption';if isPublishedProp(Form1,s) then
      SetPropValue(Form1,s,'标题');
      

  2.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      s: string;
    begin
      s := 'Caption';
      if GetPropInfo(Form1, s)<>nil then
        SetPropValue(Form1, s, '标题');
    end;
      

  3.   

    uses
      typinfo;
    procedure TForm1.btn3Click(Sender: TObject);
    var
      PropInfo:PPropInfo;
      S:string;
    begin
      s:='Caption';
      PropInfo:=GetPropInfo(Form1, S,tkProperties);
      if PropInfo=nil then
        ShowMessage('Not found')
      else
        SetPropValue(Form1,'Caption', '标题');
    end;
      

  4.   

    不知道我理解错lz的意思没有~
    最简单的方法~
    在TForm1类的public部分加入
    property S: string write PutCaption;
    然后在private部分定义
    procedure PutCaption(S: string);
    代码如下
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        procedure PutCaption(const S: string);
        { Private declarations }
      public
      published
        property Caption;
        property S: string write PutCaption;
        { Public declarations }
      end;procedure TForm1.PutCaption(const S: string);
    begin
      Caption := S;
    end;然后就可以用Form1.S := 's';改变标题~
      

  5.   

    在定义一个类的时候可能会用到属性~
    改变和读取属性值是通过相应的函数或者过程来实现的~
    通过read 和 write将属性指向相关的功能函数~

    TMyClass = class
    private
      {Private}
      ....
      FA: string; //A对应的私有成员
      procedure PutA(S: string); //写属性,注意参数类型
      function GetA(): string; //读属性,注意返回值类型
      ...
    public
      property A: string read GetA write PutA; //属性,注意类型;read一定要在write前面
    end;{功能代码不要忘了}function TMyClass.GetA(): string;
    begin
      Result := FA;
    end;procedure TMyClass.PutA(S: string): string;
    begin
      FA := S;
    end;