关于怎样给控件组赋值的问题!(100分)

S

sarny

Unregistered / Unconfirmed
GUEST, unregistred user!
我想给一个Tedit控件组(可动态生成)赋值,
例如把一个字符串数组赋值给一个edit1..edit16控件组!
请问怎样赋值
 
你可以设TEdit的Tag属性,名称可以不同,再根据Tag取相对应数组值
 
下面给一个给text属性赋值的例子
for i:=1 to 16 do
if self.findcomponent('edit'+inttostr(i))<>nil then
(self.findcomponent('edit'+inttostr(i)) as tedit).text:='xxxxx';
 
最方便的方法是声明一个控件数组:
var
Form1: TForm1;
implementation
var
texts:array[0..1] of TEdit;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
edit1,edit2:tedit;
begin
edit1:= tedit.Create(application);
texts[0]:= edit1;
texts[0].Parent:= form1;
texts[0].Top:=10;
texts[0].left:=10;
edit2:= tedit.Create(application);
texts[1]:= edit2;
texts[1].Top:=50;
texts[1].left:=10;
texts[1].Parent:= form1;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
texts[0].text:='第1个控件';
texts[1].text:='第2个控件';
end;

end.
 
to 无所居
这样作我要16个控件组,就是要把这段代码复制16篇,这显然也太烦了,
初使化就要几百行代码,能不能省啊
 
干吗不按我提供的方法试试?根本不用这么麻烦!
 
to sarny,
Admy的方法正确
其实,还可以处理不定个数的TEdit
for i:=0 to ComponentCount-1 do
if self.findcomponent('edit'+inttostr(i))<>nil then
(self.findcomponent('edit'+inttostr(i)) as tedit).text:='xxxxx';
要求你的命名从0开始,即可
 
to admy
不好意思了!hehe
不是我不用啊,是我还不太明白你代码的意思了,你能不能给完整一点的代码啊
还有一点我用16个控件“一”排开,宽为20,每一个控件放一字符串中的一个字,
谢了
 
admy说的对阿
DELPHI没有象VB一样的控件数组
用这种方法是查找所有的EDIT控件赋值的方法
 
那就写个过程
procedure init(tedit)
begin
tedit.text ="";
end
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
for i:=1 to 16 do
with tedit.create(self) do
begin
parent:=self;
left:=60*i;
top:=20;
width:=58;
name:='MyEdit'+inttostr(i);
end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
begin
for i:=1 to 16 do
if self.findcomponent('Myedit'+inttostr(i))<>nil then
(self.findcomponent('Myedit'+inttostr(i)) as tedit).text:=inttostr(i);
end;

end.

 
接受答案了.
 
顶部