救命啊,我完了!!!

fifi 2000-04-26 06:38:00
在窗口中有一批控件,如:Label1、label2、label3......(若9个);
我要通过循环改变他们的caption都为‘确定’;

var
i:integer;
s:string;
begin
for i:=1 to 9 do
begin
s:='label'+inttostr(i);
//下面该怎么实现?
x.caption:='确定'; //小弟在此无从下手,请大虾指教其中X该怎么表示?
end;
end;


小弟在此不胜感激,谢谢!

...全文
434 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinghao 2000-04-29
  • 打赏
  • 举报
回复
记不清了,好象还有一个 FindCompoments(CompomentName: string);的函数。
查帮助吧。
var
i :integer;
Labelname:string;
For i:=1 to 10 do
begin
labelname:='label'+inttostr(i);
Tlabel(FindCompoments(labelname)).Caption:='确定‘;
end;
xiaotao 2000-04-28
  • 打赏
  • 举报
回复
建立一个指针数组
type TMyLabelPionter=^TLabel
var
MyLabelPionterArray:array[1..10] of TMyLabelPionter
创建时付值如
MyLabelPionterArray[1]:=@label1
然后运用自如了
如有可取之处,请给点小分,提高点积极性嘛,


halfdream 2000-04-28
  • 打赏
  • 举报
回复
呵, 这个问题这么热。zensst,929,DreamChao几位大侠,
程序地道,风格各异。
另外,bpc的程序该是不小心弄错了吧。
Fancy_Zero 2000-04-28
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
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
fancypurelemon@kali.com.cn
DreamChao 2000-04-27
  • 打赏
  • 举报
回复
var
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;

929 2000-04-27
  • 打赏
  • 举报
回复
unit Unit1;

interface

uses
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.
bpc 2000-04-27
  • 打赏
  • 举报
回复
var
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;

RIAEasy 2000-04-27
  • 打赏
  • 举报
回复
在设计时将你要动态改变的TLabel的Tag属性设为某个值,如101;如果你要改变
所有的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;


wdh 2000-04-26
  • 打赏
  • 举报
回复
可以这样实现:
首先设置Label1.tag=1,Lable2.tag=2...
然后在用TFrom.Controls列举窗体上的控件,判断控件类型是否为TLable(用is),再根据各个Lable的tag值判断是哪一个控件,再设置相应的Caption值。
kxy 2000-04-26
  • 打赏
  • 举报
回复
var
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;
fifi 2000-04-26
  • 打赏
  • 举报
回复
问题就出在,事先表单上已经有这些控件,再create就会出错,怎么办?
Lin 2000-04-26
  • 打赏
  • 举报
回复
在戴妃中可不支持宏替换哦。可以用一个控件数组实现:
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;

剩下的就不用我说了。

5,386

社区成员

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

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