用form里面固定button创建动态的button.在结束此程序时,下次打开时动态BUTTON还有在并不消失......要有哪种文件型保存动态BUTTON.

解决方案 »

  1.   

    1.動態創建button:procedure   TForm1.Button1Click(Sender:   TObject); 
    var 
        button2:tbutton; 
    begin 
        button2:=tbutton.create(self); 
        button2.Parent:=form1; 
        button2.Caption:= 'heoo '; 
        button2.Left:=100; 
        button2.Top:=100; 
        button2.Height:=100; 
        button2.Width:=100; 
        button2.show; 
    end;
      

  2.   

    2.完整的:unit Unit1; 
    interfaceuses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls;type
    TForm1 = class(TForm)
        btnAddButton: TButton;
        btnDeleteLast: TButton;
        procedure btnAddButtonClick(Sender: TObject);
        procedure btnDeleteLastClick(Sender: TObject);
    private
        { Private declarations }
        procedure CustomButtonClick(Sender: TObject);
    public
        { Public declarations }
    end;var
    Form1: TForm1;implementation{$R *.dfm}procedure TForm1.btnAddButtonClick(Sender: TObject);
    var
    NewButton: TButton;   // 新 Button的指针
    begin
    // 在内存中创建一个 Button,拥有者为self,这样当窗体 destory时,这个新button
    // 能够被自动释放
    NewButton := TButton.Create(Self);With NewButton do
    begin
        Top := 60;          // button 的出现的坐标
        Width := 60;        // button 的宽度
        Left := Width * (Self.ControlCount - 2);
        Parent := Self;     // 指明在那个窗体显示
        OnClick := CustomButtonClick;       // 指定button click事件
        Caption := 'Button' + IntToStr(Self.ControlCount - 2);
    end; // with
    end;procedure TForm1.btnDeleteLastClick(Sender: TObject);
    begin
    // 确定窗体上有新的button
    if Self.ControlCount > 2 then
        // 删除最后新建的 button
        TButton(Controls[ControlCount - 1]).Destroy;
    end;procedure TForm1.CustomButtonClick(Sender: TObject);
    begin
    // 根据 Sender 来判断哪个新建的button click
    ShowMessage(TButton(Sender).Caption + ' Pressed');
    end;end
     
      

  3.   

    3.下次打開程序動態創建的button還在?這裡提供個思路,只供LZ參考:
    a.在窗體create時,動態去執行創建button的事件;
    b.可編寫一動態創建button過程,在show時調用;
    c.可將上次動態創建的button所有屬性值寫入ini文件,下次打開時根據ini值再創建還原回來...
      

  4.   

    Delphi有持久化机制,TStream、TWriter、TReader可以用ReadComponent、WriteComponent方法来读取和写入控件