delphi 程序生成控件及可变的控件名

如果是调用来自另一个FORM的控件,则: FORM2.FindComponent(‘Label1′);

procedure TForm1.Button3Click(Sender: TObject);
var i: smallint;
var tb: TbitBtn;
begin
//      for i := 1 to ComponentCount-1 do
for i := 1 to 5 do
begin
tb := TBitBtn(FindComponent('TB' + IntToStr(i)));
tb.Caption := 'tttt';
end;

end;

以下片段来自:

delphi 可变控件

http://www.hur.cn/special/delphiProgram/06227.htm

VARIABLE CONTROL-NAMES

华软源码

Here is a sample code I just made to test dynamic component creation;
I hope this helps you…:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Menus, StdCtrls, Buttons;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
arr : array [1..30] of TBitBtn;
procedure BitButtonClick(Sender: TObject);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.BitButtonClick(Sender: TObject);
begin
if TBitBtn(Sender).Font.Style = [] then
begin
TBitBtn(Sender).Font.Style := [fsBold];
TBitBtn(Sender).Font.Color := clRed;
end
else
begin
TBitBtn(Sender).Font.Style := [];
TBitBtn(Sender).Font.Color := clBlack;
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
x, y,
f : integer;
begin
x := 20;
y := 20;
for f := 1 to 30 do
begin
arr [f] := TBitBtn.Create (Form1);
if arr [f] <> nil then
begin
arr [f].Parent := Form1;
arr [f].left := x;
arr [f].top := y;
arr [f].width := 35;
arr [f].Height := 25;
arr [f].caption := IntToStr (f);
arr [f].tag := f;
arr [f].OnClick := BitButtonClick;
inc (x, arr [f].width + 20);
if (x + arr [f].width) > form1.width then
begin
x := 20;
inc (y, arr [f].height + 20);
end;
end;
end;
end;

end.

A:
There are two ways I can think of:

(1) Using FindComponent. Will have to use the components name:

var
lab: TLabel;
begin
lab := TLabel(FindComponent(‘Label1′));
lab.Caption := ‘Hey!’
end;

(2) Using the Tag property. Set each label’s Tag property (ie: 1 to 100)
and then
you will be able to do something like:

procedure TForm1.Button2Click(Sender: TObject);
var
i: smallint;
begin
for i := 0 to ComponentCount-1 do
if (Components[i] is TLabel) AND (Components[i].Tag = 3) then
begin
TLabel(Components[i]).Caption := ‘This is the one!’;
break;
end;
end;

A:
How about iterating through all your components (Check your form’s
Components property).
Use something like this:

var
I: Integer;
begin
for I := 0 to ComponentCount -1 do
if Components[I] is TLabel then
TLabel(Components[I]).Caption := ‘This is the one!’;
end;

A:
Put the labels into a TList. You can then use :

TLabel(mylist.items[MyValue]).Caption := ‘This is the one!’;

A:
function LabelCaption(MyValue: Integer; Text: string) : Boolean;

function LabelCaption(MyValue: Integer; Text: string) : Boolean;
var
LabelCount,
Loop: Integer;
begin
Result := False;
LabelCount := 0;
for Loop := 0 to ComponentCount – 1 do
begin
if Components[Loop] is TLabel then  { is it a label ? }
begin
INC(LabelCount);
if LabelCount = MyValue then  { does it match my index value ? }
begin
Component[Loop].Caption := Text;
Result := True;
end;
end;
end;
end;

Call it as follows:-

if LabelCaption(10, ‘This is the one!’) then
WriteLn(‘Found it!’);

The other approach which is more in line with what you want would be to
use a
StringList of some sort in which case you could do an IndexOf on the list
to find
your label. You could use the above Loop to add the label names to the
StringList.

This would allow you to do a lookup as follows:-

MyLabels[MyLabels.IndexOf(Label10)].Caption := ‘This is the one!’;

此条目发表在Program分类目录,贴了标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>