怎样用一个循环语句来给一批同类的控件的同一个属性赋值?(100分)

  • 主题发起人 wugdoneone
  • 开始时间
W

wugdoneone

Unregistered / Unconfirmed
GUEST, unregistred user!
想在程序运行中给一批同类的控件的同一个属性赋值:比如若干个label 的caption.
因为要根据程序运行中的一个整型变量的值来确定到底要给多少个label 的 caption
属性赋值,所以希望通过一个循环语句来进行.不知程序应该怎样写!

 
for i:=self.componetcount-1 downto 0 do
begin
if (self.componetcount is tlabel) then
tlabel(self.componetcount).caption:='ok';
end;
//最好把self换成你的窗体名,信手写的,没有调试,你试一下
 
procedure SetCaptions(Ctrl:array of TLabel)
var
i:Integer;
begin
for i:=Low(Ctrl) to High(Ctrl) do
Ctrl.Caption:='????';
end;
....
SetCaptions([label1,label2,label3,...]);
 
同意楼上观点,来晚了我。
 
Shuzi兄:
我照你说的做,可是程序出错:this form of method call only allowed for class methods
这是怎么回事?
 
for i:=form1.componetcount-1 downto 0 do
begin
if form1.componetcount is tlabel then
tlabel(self.componets).caption:='ok';
end;
 
wugdoneone:
能贴出你的源程序吗?

 
to :Shuzi兄
我完全照你的程序写的,一点都没有改。不过我用的是delphi5!
 
wugdoneone:
不好意思,应该是:
procedure SetCaptions(Ctrl:array of TLabel)
var
i:Integer;
begin
for i:=Low(Ctrl) to High(Ctrl) do
Ctrl.Caption:='????';
end;
 
用 className 是不是比较精确!

procedure TForm1.Button4Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to componentcount-1 do
if uppercase(controls.ClassName)='TBUTTON' then
TButton(controls).caption:='abcd';
end;
 
那位信手写来的王兄其实做法是对的,思路也是对的,就是懒了点,:)
我写出来调过的程序,试试吧,我想这么做是可以的
procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;
begin
for i:=self.ComponentCount-1 downto 0 do
begin
if (self.Components is tlabel) then
tlabel(self.Components).caption:='ok';
end;
end;
当然把self.ComponentCount-1 downto 0 do
改为0 to self.ComponentCount-1 do也可,我这么写只是说明,人家的才是原版,免得
王公子跟我拼命呀!
 
谢谢各位!!!
 
顶部