星期四, 12月 13, 2007

動態為所有TLabel加Caption上去

這個功能主要是給,你一次有太多的TLabel要用for迴圈給值,但是你又不想用動能新增的方式,因為每個位置如果都差異很大,還要一個一個給,所以你就可以用讀入form內所有的物件,在此我又針對爸爸在在Tabsheet1才去判斷。


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, TypInfo, ComCtrls;

const
lb : array [1..4, 1..2] of string = (('Label1', '3'), ('Label2', '29'), ('Label3', '63'), ('Label4', '35'));

type
TForm1 = class(TForm)
Button1: TButton;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
i,j : integer;
sStr : string;
proInfo : PPropInfo;
begin
j:=1;

for i:=0 to Componentcount-1 do
begin
if TControl(Components[i]).Parent = TabSheet1 then //找他爸
begin
proInfo := GetPropInfo(Components[i].ClassInfo, 'Caption'); //得到有Caption的物件
if (proInfo <> nil) then
begin
if Components[i].name = lb[j][1] then
begin
sStr := inttostr(j);
SetStrProp(Components[i], proInfo, sStr);
j:=j+1;
end;
end;
end;
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin

showmessage(inttostr(TabSheet1.ComponentCount));
end;

end.

沒有留言: