Delphi属性设置问题?
如果性属存在一个变量中,Delphi如何设置这个变量啊,如:
var s:='Caption';
怎么通过s设置一个From的Caption属性? 如:
form1.s:='标题';
问题点数:100、回复次数:7Top
1 楼slatly(惜唐琬)回复于 2006-03-04 14:57:23 得分 0
呵呵 感觉难度挺大的 帮顶一下Top
2 楼zhongme_007(zhongme_007)回复于 2006-03-04 15:06:58 得分 0
帮你顶Top
3 楼windindance(风舞轻扬·白首为功名)回复于 2006-03-04 15:21:54 得分 20
use Typinfo;
var s :string;
s :='Caption';
if isPublishedProp(Form1,s) then
SetPropValue(Form1,s,'标题');Top
4 楼cuteant(我这张旧床票还能否登上你的破床|涛声是否依旧)回复于 2006-03-04 15:28:55 得分 30
procedure TForm1.Button1Click(Sender: TObject);
var
s: string;
begin
s := 'Caption';
if GetPropInfo(Form1, s)<>nil then
SetPropValue(Form1, s, '标题');
end;Top
5 楼cncharles(旺仔)回复于 2006-03-04 16:35:49 得分 30
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;
Top
6 楼AKFish(秋天的刀鱼.色狼)回复于 2006-03-04 20:11:32 得分 20
不知道我理解错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';改变标题~Top
7 楼AKFish(秋天的刀鱼.色狼)回复于 2006-03-04 20:20:51 得分 0
在定义一个类的时候可能会用到属性~
改变和读取属性值是通过相应的函数或者过程来实现的~
通过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;Top




