例如我想生成一定数量的Edit,只需输入其数量,程序就自动生成需要个数的Edit。
谢谢!

解决方案 »

  1.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ExtCtrls, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Panel1: TPanel;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}
    //生成,这里用了3 个Edit
    procedure TForm1.Button1Click(Sender: TObject);
    var
      i:integer;
    begin
     for i:= 1 to 3 do
      with TEdit.Create(panel1) do
      begin
           Left:=100;
           Top:=30*i;
           Tag:=1;
           Parent:=panel1;
           text:='MyEdit'+IntToStr(i);
      end;end;
    //释放
    procedure TForm1.Button2Click(Sender: TObject);
    var
      j:integer ;
    begin
      with panel1 do
      for j:=ControlCount-1 downto 0 do
      if Controls[j].Tag=1 then
       //Controls[j].Free;
    end;end.
      

  2.   

    为什么下面这段程序只能创建Edit,而不能创建Label?
    procedure TForm1.BitBtn1Click(Sender: TObject);
    var
      i, AmountInt, ComponentType : Integer;
      Amount : string;
    begin
      ComponentType := ComboBox1.ItemIndex;
      Amount        := Trim(Edit1.Text);
      if Amount = '' then
        MessageDlg('"Amount" can not empty!', MTWarning, [MBOk], 0)
      else
        try AmountInt := StrToInt(Amount);
          if AmountInt > 0 then
            if AmountInt > 10 then
              MessageDlg('"Amount" is too big!', MTWarning, [MBOk], 0)
            else
              case ComponentType  of
                0:
                   for i := 1 to AmountInt do
                     with TLabel.Create(Panel1) do
                       begin
                         Left   := 100;
                         Top    := 30 * i;
                         Tag    := 1;
                         Parent := Panel1;
                         Text   := 'Lable' + IntToStr(i);
                       end;
                1:
                   for i := 1 to AmountInt do
                     with TEdit.Create(Panel1) do
                       begin
                         Left   := 100;
                         Top    := 30 * i;
                         Tag    := 1;
                         Parent := Panel1;
                         Text   := 'Edit' + IntToStr(i);
                       end
               else
                 MessageDlg('No this Component Type!', MTWarning, [MBOk], 0);
               end
          else
            MessageDlg('"Amount" is only plus integer!', MTWarning, [MBOk], 0);
        except
          MessageDlg('"Amount" is only plus integer!', MTWarning, [MBOk], 0);
        end;
    end;