新建一个Application。
在Form1上放两个TButton,一个TListBox;
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ListBox1: TListBox;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
FTop: Integer;
procedure CreateEdit;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Button1Click(Sender: TObject);
var I: Integer; S: string;
begin
ListBox1.Clear;
for I:=0 to Self.ControlCount-1
do begin
if (Self.Controls is TEdit)and(TEdit(Self.Controls).Tag=101)
then begin
S:=TEdit(Self.Controls).Text;
ListBox1.Items.Add(S);
end;
end;
end;
procedure TForm1.CreateEdit;
var ED: TEdit;
begin
ED:=TEdit.Create(Self);
ED.Parent:=Self;
ED.Tag:=101;
ED.Name:='ED'+IntToStr(Self.ControlCount);
ED.Text:=ED.Name;
Inc(FTop, 30);
ED.Top:=FTop;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
FTop:=10;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Self.CreateEdit;
end;
end.