在Delphi中使用变量作为对象名称 [英] Use variables for object name in Delphi

查看:117
本文介绍了在Delphi中使用变量作为对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用常规方式更改许多labels的标题:

I'm trying to change the caption of many labels using regular way:

form1.label1.caption := '1';
form1.label2.caption := '2';
form1.label3.caption := '3';
form1.label4.caption := '4';
form1.label5.caption := '5';
form1.label6.caption := '6';
form1.label7.caption := '7';
form1.label8.caption := '8';
...

如何使用For并将i分配给像Label[i]这样的标签名称?像这样:

How can I use For and assign i to label name like Label[i]? Something like this:

for i := 1 to 50 do
   begin
     form1.label[i].caption := Inttostr(i);
   end;

什么是更改太多对象参数(在本例中为caption)的最佳方法?

And what is the best way to change too many object parameters (In this case caption)?

推荐答案

动态创建控件.如果需要保留对它们的引用,请将这些引用保存在数组中.例如,这是常规模式.

Create your controls dynamically. If you need to retain references to them hold those references in an array. For example, this is the general pattern.

var
  FLabels: array of TLabel;
....
SetLength(FLabels, Count);
for i := 0 to Count-1 do
begin
  FLabels[i] := TLabel.Create(Self);
  FLabels[i].Parent := Self;
  FLabels[i].Caption := IntToStr(i+1);
  FLabels[i].Left := 8;
  FLabels[i].Top := 8 + i*20;
end;

这篇关于在Delphi中使用变量作为对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆