在戴妃中可不支持宏替换哦。可以用一个控件数组实现:
var Labels: array[0..9] of TLabel;
其实最好在Form的OnCreate事件中动态创建这样的一个数组就行:
procedure TForm1.OnCreate(Sender: TObject);
var nLoop: Integer;
begin
for nLoop := 0 to 9 do
begin
...
Labels[nLoop] := TLabel.Create(Self);
Labels[nLoop].Parent := Self;
Labels[nLoop].Left := xxxx;
Labels[nLoop].Top := yyyy;
Labels[nLoop].Name := 'Label' + IntToStr(nLoop);
...
end;
end;剩下的就不用我说了。
var Labels: array[0..9] of TLabel;
其实最好在Form的OnCreate事件中动态创建这样的一个数组就行:
procedure TForm1.OnCreate(Sender: TObject);
var nLoop: Integer;
begin
for nLoop := 0 to 9 do
begin
...
Labels[nLoop] := TLabel.Create(Self);
Labels[nLoop].Parent := Self;
Labels[nLoop].Left := xxxx;
Labels[nLoop].Top := yyyy;
Labels[nLoop].Name := 'Label' + IntToStr(nLoop);
...
end;
end;剩下的就不用我说了。
I : integer
TC : TCompoent
begin
for i:=0 to ComponetCount-1 do
beign
if Componets[I].Name = 'Label'+IntToStr(i+1) then
begin
Componets[I].Caption := '确定';
end;
end;
end;
首先设置Label1.tag=1,Lable2.tag=2...
然后在用TFrom.Controls列举窗体上的控件,判断控件类型是否为TLable(用is),再根据各个Lable的tag值判断是哪一个控件,再设置相应的Caption值。
所有的TLabel,则没有必要。
procedure ChangeLabel;
var I:Integer;
begin
for I:=O to Self.ControlCount-1
do begin
if Self.Control[I].Tag=101 //或者 if (Self.Control[I] is TLabel)
then begin
try
TLabel(Self.Control[I]).Caption:='确定';
except
end;
end;
end;
end;
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
temp:TComponent;
begin
for i:=0 to form1.ComponentCount-1 do
begin
temp:=form1.Components[i];
//showmessage(temp.classname);
if temp.ClassName='TLabel' then
(temp as TLabel).caption:='确定';
end;
end;end.
i:integer;
tmpComponent:TComponent;
begin
for i:=0 to ComponentCount-1 do
begin
tmpComponent:=Components[i];
if tmpComponent.ClassName='TLabel' then
(TLabel)tmpComponent.caption:='确定';
end;
end;
i:integer;
tmpComponent:TComponent;
begin
for i:=0 to appication.ComponentCount-1 do
begin
tmpComponent:=appication.Components[i];
(application.Component[i] as Tlable).caption:='确定';
end;
end;
程序地道,风格各异。
另外,bpc的程序该是不小心弄错了吧。
type TMyLabelPionter=^TLabel
var
MyLabelPionterArray:array[1..10] of TMyLabelPionter
创建时付值如
MyLabelPionterArray[1]:=@label1
然后运用自如了
如有可取之处,请给点小分,提高点积极性嘛,
var i:integer;
begin
for i := 0 to form1.ComponentCount-1 do
begin
if form1.components[i]is Tlabel then
begin
// if Tlabel(form1.compoments[i]).Name=......
Tlabel(form1.Components[i]).Caption:='OK!OK!';
end;
end;
end;i assume there is 10 label in form1 and have a button1
[email protected]
查帮助吧。
var
i :integer;
Labelname:string;
For i:=1 to 10 do
begin
labelname:='label'+inttostr(i);
Tlabel(FindCompoments(labelname)).Caption:='确定‘;
end;