请问如何给一个对象的好多属性付值?(50分)

  • 主题发起人 主题发起人 lzjnuaa
  • 开始时间 开始时间
L

lzjnuaa

Unregistered / Unconfirmed
GUEST, unregistred user!
如一个Class有caption1,caption2,caption3,caption4等属性
我想用一个循环一起赋值,请指教
 
用with语句
 
for i:=1 to 4 do
begin
self.caption(此处怎么写):='ok';
end;
 
用RTTI, D5开发人员指南有例子
 
我这里没有这书,急需解决,请哪位大虾贴出来,谢谢
 
使用C:/Program Files/Borland/Delphi6/Source/Rtl/Common/typinfo.pas
unit typinfo;
procedure SetPropValue(Instance: TObject; const PropName: string;
const Value: Variant);

例:
for i:=1 to 4 do
begin
SetPropValue(self,'caption'+IntToStr(i),'ok');
end;
 
unit MainFrm;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ExtCtrls;

type
TMainForm = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Panel1: TPanel;
Button5: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Panel1Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
MainForm: TMainForm;

implementation
uses TypInfo;
{$R *.DFM}

procedure SetIntegerPropertyIfExists(AComp: TComponent; APropName: String;
AValue: Integer);
var
PropInfo: PPropInfo;
begin
PropInfo := GetPropInfo(AComp.ClassInfo, APropName);
if PropInfo <> nil then
begin
if PropInfo^.PropType^.Kind = tkInteger then
SetOrdProp(AComp, PropInfo, AValue);
end;
end;

procedure SetObjectPropertyIfExists(AComponent: TComponent; APropName: String;
AValue: TObject);
var
PropInfo: PPropInfo;
begin
PropInfo := GetPropInfo(AComponent.ClassInfo, APropName);
if PropInfo <> nil then
begin
if PropInfo^.PropType^.Kind = tkClass then
SetObjectProp(AComponent, PropInfo, AValue);
end;
end;

procedure SetBooleanPropertyIfExists(AComp: TComponent; APropName: String;
AValue: Boolean);
var
PropInfo: PPropInfo;
begin
PropInfo := GetPropInfo(AComp.ClassInfo, APropName);
if PropInfo <> nil then
begin
if PropInfo^.PropType^.Kind = tkEnumeration then
SetOrdProp(AComp, PropInfo, Integer(AValue));
end;
end;

procedure SetStringPropertyIfExists(AComp: TComponent; APropName: String;
AValue: String);
var
PropInfo: PPropInfo;
TK: TTypeKind;
begin
PropInfo := GetPropInfo(AComp.ClassInfo, APropName);
if PropInfo <> nil then
begin
TK := PropInfo^.PropType^.Kind;
if (TK = tkString) or (TK = tkLString) or (TK = tkWString) then
SetStrProp(AComp, PropInfo, AValue);
end;
end;

procedure SetMethodPropertyIfExists(AComp: TComponent; APropName: String;
AMethod: TMethod);
var
PropInfo: PPropInfo;
begin
PropInfo := GetPropInfo(AComp.ClassInfo, APropName);
if PropInfo <> nil then
begin
if PropInfo^.PropType^.Kind = tkMethod then
SetMethodProp(AComp, PropInfo, AMethod);
end;
end;

procedure TMainForm.Button1Click(Sender: TObject);
begin
SetStringPropertyIfExists(Button1, 'Caption', 'Yahoo');
end;

procedure TMainForm.Button2Click(Sender: TObject);
begin
SetIntegerPropertyIfExists(Button2, 'Width', 50);
end;

procedure TMainForm.Button3Click(Sender: TObject);
begin
SetBooleanPropertyIfExists(Button3, 'Enabled', False);
end;

procedure TMainForm.Button4Click(Sender: TObject);
var
F: TFont;
begin
F := TFont.Create;
F.Name := 'Arial';
F.Size := 24;
F.Color := clRed;
SetObjectPropertyIfExists(Panel1, 'Font', F);
end;

procedure TMainForm.Button5Click(Sender: TObject);
begin
SetMethodPropertyIfExists(Button5, 'OnClick',
GetMethodProp(Panel1, 'OnClick'));
end;

procedure TMainForm.Panel1Click(Sender: TObject);
begin
ShowMessage(Button5.Caption);
end;


end.

 
后退
顶部