delphi 使用TFileStream流实现对象持久性  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi 使用TFileStream流实现对象持久性



unit main;


interface


uses

  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

  StdCtrls, Grids;


type

  TForm1 = class(TForm)

    ListBox1: TListBox;

    Edit1: TEdit;

    Memo1: TMemo;

    procedure Edit1KeyPress(Sender: TObject; var Key: Char);

    procedure FormCreate(Sender: TObject);

    procedure FormClose(Sender: TObject; var Action: TCloseAction);

    procedure ListBox1DblClick(Sender: TObject);

  private

    { Private declarations }

  public

    { Public declarations }

  end;


var

  Form1: TForm1;


implementation


{$R *.DFM}


procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);

begin

  if Key = #13 then begin

    Key := #0;

    ListBox1.Items.Add(Edit1.Text);

    Edit1.Text := '';

  end;

end;


procedure TForm1.FormCreate(Sender: TObject);

var

  strm : TFileStream;

begin

  if FileExists('MyList.DAT') then begin

    strm := TFileStream.Create('MyList.DAT', fmOpenRead);

    strm.ReadComponent(ListBox1);

    strm.Free;

  end;

end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);

var

  strm : TFileStream;

begin

  strm := TFileStream.Create('MyList.DAT', fmCreate);

  strm.WriteComponent(ListBox1);

  strm.Free;

end;


procedure TForm1.ListBox1DblClick(Sender: TObject);

begin

  ListBox1.Items.Delete(ListBox1.ItemIndex);

end;


end.


来源:http://www.delphicorner.f9.co.uk/articles/comps5.htm


推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.19410014152527 seconds