用FOR循环给成批给控件属性赋值的进一步问题?

boyuan 2003-05-29 11:24:02
上一次的问题如下:
我在一个窗体上定义了12个LABEL控件,分别为LABEL1----LABEL12,现在想用FOR循环给它们的一个相同的属性赋以相同的值,我的想法是这样的:
for i:=1 to 12 do
begin
label[i].visible:=false;
end;
但这样做显然不行,请问我如何才能在实例的名字中使用一个变量?

霹雳猫(DELPHI迷)的答案如下:
先给这些需要成批处理的Lable的Tag属性设置为一个统一的值,如3
例:
for i:=0 to ComponentCount-1 do
if components[i] is TLable then
if (Components[i] as TLable).Tag=3 then
(Components[i] as TLable).Visible:=false;
我已经试过,的确可以达到把所有TLABEL控件成批赋值。

现在,有一个更进一步的需求:
我要根据一个变量N的指示,把从LABELN+1到LABEL12的VISIBLE属性改为FALSE,如何改动以上代码?
另外,我查了一下DELPHI的帮助,没有看懂COMPONENT这个东西的含义?麻烦哪位贤人再多费口舌,解释清楚一点。
...全文
115 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
microjuz 2003-05-30
  • 打赏
  • 举报
回复
for i:=n to ComponentCount-1 do
我还没有试过,它是按什么顺序遍历下去的??申明的顺序?
我明天回来试一下,继续讨论,`~~~keke,,睡觉
boyuan 2003-05-30
  • 打赏
  • 举报
回复
谢谢小弟弟,我自己搞定了!这话怎么不太对劲??
告诉你一个更聪明的办法吧:
for i:=1 to 12 do
begin
TLabel(FindComponent('Label' + inttostr(i))).Visible := False;
end;
想怎么遍历怎么遍历。
microjuz 2003-05-30
  • 打赏
  • 举报
回复
试过了,遍历的顺序是按某种顺序,我得出的是tlable-》tbutton-》tedit
其他的就没有试了,你的问题搞定了没有
microjuz 2003-05-29
  • 打赏
  • 举报
回复
这样好了,你不是有12个edit和label吗
给每个edit赋不同的tag值,从0到11,tag是整型的,你可以用判断语句>或者<来进行过滤
keke,觉的怎么样,应该可以解决你这个问题吧
boyuan 2003-05-29
  • 打赏
  • 举报
回复
我正是要动态的改变N,N来自一个LISTBOX,随着选择的值不同,要隐藏LABELN+1以后的控件,现在又有新问题了,除了LABEL1---12以外,还有EDIT1---12,也要象LABEL一样的隐藏,该如何设置EDIT1---12的TEG属性?我套用了以上的程序想同时隐藏LABEL和EDIT,但为何LABEL能实现,而EDIT却不加选择的全部隐藏呢?
我用的程序如下:
procedure Tsettankform.ComboBox1Change(Sender: TObject);
var
n:integer;
i:integer;
begin
n:=strtoint(combobox1.Text);
for i:=1 to componentcount-1 do
if components[i] is tlabel then
if (components[i] as tlabel).tag=3 then
(components[i] as tlabel).Visible:=true;
for i:=n+3 to ComponentCount-1 do //因为面版上还有另外两个LABEL控件,所以从n+3开始。
if components[i] is TLabel then
if (Components[i] as TLabel).Tag=3 then
(Components[i] as TLabel).Visible:=false;

for i:=0 to componentcount-1 do
if components[i] is tedit then
if (components[i] as tedit).tag=2 then
(components[i] as tedit).Visible:=true;
for i:=n to ComponentCount-1 do
if components[i] is tedit then
if (Components[i] as Tedit).Tag=2 then
(Components[i] as tedit).Visible:=false;
end;
end.
microjuz 2003-05-29
  • 打赏
  • 举报
回复
你查tcomponent关键字
Components are persistent objects that have the following capabilities:

The ability to appear on Component palette and be manipulated in the form designer.
The ability to own and manage other components.
Enhanced streaming and filing capabilities.
The ability to be converted into an ActiveX control or other COM object by wizards on the ActiveX page of the New Objects dialog.

从上面知道,component的对父类tpersistent的一些功能上的扩展

vcl树,tobject-tpersistent-tcomponent(如果想要完全版的话给我发消息)

如果要改动从LABELN+1到LABEL12的VISIBLE属性改为FALSE
我觉的还是从tag属性入手,~~你该不会要动态的改变N吧

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧