DELPHI 动态 创建和释放 多个 EDIT 控件

时间:2021-05-23 09:12:15
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;
 
type
  TForm1 = class(TForm)
    pnl1: TPanel;
    btn1: TButton;
    btn2: TButton;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
  editm:array of tedit;
 
implementation
 
{$R *.dfm}
 
//动态创建EDIT控件
procedure TForm1.btn1Click(Sender: TObject);
var
    i,d,j:integer;
begin
d:=;
j:=;
setlength(editm,j);
  for   i:=   to   j-   do
  begin
    editm[i]:=tedit.Create(self);
    editm[i].Parent:=pnl1;
    editm[i].Width:=;
    editm[i].Height:=;
    editm[i].Left:=;
    editm[i].Top:=+d;
    editm[i].Name:= 'edit'+inttostr(i);
    editm[i].Text:= 'edit'+inttostr(i);
    editm[i].Visible:=true;
    d:=d+;
  end;
end;
//销毁创建的EDIT控件
procedure TForm1.btn2Click(Sender: TObject);
var
  i:Integer;
begin
for i:= to do
begin
editm[i].free;
end;
end;
 
end.