ini写配置信息 列表框

 
 
 
unit Unit1; 

interface 

uses 
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls; 

type 
  TForm1 = class(TForm) 
    ListBox1: TListBox; 
    Button1: TButton; 
    Button2: TButton; 
    Memo1: TMemo; 
    Button3: TButton; 
    Button4: TButton; 
    procedure Button3Click(Sender: TObject); 
    procedure Button4Click(Sender: TObject); 
    procedure Button1Click(Sender: TObject); 
    procedure Button2Click(Sender: TObject); 
  private 
    { Private declarations } 
  public 
    { Public declarations } 
  end

var 
  Form1: TForm1; 

implementation 
 uses IniFiles; 
{$R *.dfm} 

var iniPath:string;//ini路径 
    fName:string;  //ini的文件名 
procedure ListBoxToINI(const AINIFile, ASection: string; ListBox: TListBox); 
var 
  INI: TINIFile; 
  I: Integer; 
begin 
  INI := TINIFile.Create(AINIFile); 
  try 
    INI.EraseSection(ASection); 
    INI.WriteInteger(ASection, 'Count', ListBox.Items.Count); 
    for I := 0 to ListBox.Items.Count - 1 do 
    begin 
      INI.WriteString(ASection, 'Item' + IntToStr(I), ListBox.Items[I]); 
    end
  finally 
    INI.Free; 
  end
end

procedure INIToListBox(const AINIFile, ASection: string; ListBox: TListBox); 
var 
  INI: TINIFile; 
  I, Count: Integer; 
begin 
  INI := TINIFile.Create(AINIFile); 
  try 
    Count := INI.ReadInteger(ASection, 'Count', 0); 
    for I := 0 to Count - 1 do 
    begin 
      ListBox.Items.Add(INI.ReadString(ASection, 'Item' + IntToStr(I), '0')); 
    end
  finally 
    INI.Free; 
  end
end

{write ini} 
procedure TForm1.Button1Click(Sender: TObject); 
begin 
  fName:='A'
  iniPath:=GetCurrentDir()+format('\%s.ini',[fName]); 
  ListBoxToINI(iniPath,'列表框1',ListBox1); 
end

{read ini} 
procedure TForm1.Button2Click(Sender: TObject); 
begin 
  fName:='A'
  iniPath:=GetCurrentDir()+format('\%s.ini',[fName]); 
  INIToListBox(iniPath,'列表框1',ListBox1); 
  memo1.Text:=iniPath; 
end

procedure TForm1.Button3Click(Sender: TObject); 
begin 
 ListBox1.Items:=memo1.Lines; 
end

procedure TForm1.Button4Click(Sender: TObject); 
begin 
  listbox1.Clear; 
end

end.




附件列表

     

    posted @ 2012-05-30 19:46  XE2011  阅读(751)  评论(0编辑  收藏  举报