如何删除INI文件关键字 |
尚未結案
|
vclphi
一般會員 發表:28 回覆:39 積分:13 註冊:2003-03-06 發送簡訊給我 |
我的程序有二个Form(Form1;Form2),上面各有一个ListBox,假设我的INI文件(Stat.ini)只有一个小节([Test]),如何将其中关键字(不是关键字的值)列出在Form1.listBox1中,而在Form2.listBox1中对Stat.ini的关键字进行删除,改名等操作(操作后新INI文件要回列在Form1.listBox1中)。
将其中关键字列出在Form1.listBox1中我用了如下代码:
procedure TForm1.listTitle;
var filename:string;
i:integer;
breedno:tstringlist;
begin
listBox1.Clear;
filename:=ExtractFilePath(paramstr(0)) 'stat.ini';
inifile:=TInifile.Create(filename);
breedno:=tstringlist.create;
inifile.ReadSection('test',breedno);
for i:=0 to breedno.Count-1 do
begin
listbox1.items.add(breedno.Strings[i]);
end;
end;
在Form2.listBox1中对Stat.ini的关键字进行删除,改名等操作(操作后新INI文件要回列在Form1.listBox1中)该怎么写? 發表人 - vclphi 於 2003/05/13 11:31:39 發表人 - vclphi 於 2003/05/13 11:48:08
|
timhuang
尊榮會員 發表:78 回覆:1815 積分:1608 註冊:2002-07-15 發送簡訊給我 |
|
vclphi
一般會員 發表:28 回覆:39 積分:13 註冊:2003-03-06 發送簡訊給我 |
|
banson1716
高階會員 發表:55 回覆:182 積分:167 註冊:2002-04-14 發送簡訊給我 |
參考看看範例 unit Unit1; interface uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IniFiles; type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button2Click(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure ListBox1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure ListBox1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
Application_Path:string;
procedure SetIniFile(sList: TStrings);
{ Private declarations }
public
procedure LoadIniFile(sList: TStrings);
{ Public declarations }
end; var
Form1: TForm1; const
iniFileName='2ccc.com.ini';
SectionName='TypeStrings'; implementation {$R *.dfm}
{ 保存列表數據寫入到ini文件的函數 }
procedure TForm1.SetIniFile(sList:TStrings);
var
i:Integer;
f:TIniFile;
begin
f:=TIniFile.Create(Application_Path+iniFileName); {建立ini文件}
try
{刪除原來的數據}
if f.SectionExists(SectionName) then f.EraseSection(SectionName);
if sList.Count>0 then
begin
for i:=0 to sList.Count-1 do {循環寫入數據}
f.WriteString(SectionName,'Value'+IntToStr(i),sList.Strings[i]);
end;
finally
f.Free;
end;
end;
{ 從ini文件讀出列表數據的函數 }
procedure TForm1.LoadIniFile(sList:TStrings);
var
i:Integer;
f:TIniFile;
ss:TStrings;
begin
f:=TIniFile.Create(Application_Path+iniFileName);
try
if f.SectionExists(SectionName) then
begin
ss:=TStringList.Create;
try
f.ReadSection(SectionName,ss); {讀入全部列表數據名}
if ss.Count>0 then
begin
for i:=0 to ss.Count-1 do {循環讀入數據}
sList.Add(f.ReadString(SectionName,ss.Strings[i],''));
end;
finally
ss.Free;
end;
end;
finally
f.Free;
end;
end; procedure TForm1.Button1Click(Sender: TObject);
var {添加數據}
i:Integer;
b:Boolean;
begin
if Edit1.Text='' then Exit;
b:=True;
if ListBox1.Items.Count>0 then
for i:=0 to ListBox1.Items.Count-1 do
if Edit1.Text=ListBox1.Items.Strings[i] then b:=False;
if b then
begin
ListBox1.Items.Add(Edit1.Text);
Edit1.Text:=#0;//設為 Edit1.Enabled:=False;
end;
end; procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end; procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
SetIniFile(ListBox1.Items);
end; procedure TForm1.Button2Click(Sender: TObject);
begin
ListBox1.Items.Delete(ListBox1.ItemIndex);
Button2.Enabled:=False;
end; procedure TForm1.ListBox1Click(Sender: TObject);
begin
Button1.Default:=False;
Button2.Enabled:=True;
end; procedure TForm1.Edit1Change(Sender: TObject);
begin
if (Sender as TEdit).Text<>'' then
begin
Button1.Default:=True;
Button1.Enabled:=True;
end else
begin
Button1.Default:=False;
Button1.Enabled:=False;
end;
end; procedure TForm1.ListBox1DragDrop(Sender, Source: TObject;
X, Y: Integer);
var {列表框拖放處理}
aPoint:TPoint;
begin
aPoint.x:=X;
aPoint.y:=Y;
if ListBox1.ItemAtPos(aPoint,True)<>-1 then {是否超出最後一個的範圍}
ListBox1.Items.Exchange(ListBox1.ItemIndex,ListBox1.ItemAtPos(aPoint,True))
else
ListBox1.Items.Exchange(ListBox1.ItemIndex,ListBox1.Items.Count-1);
end; { 拖放時滾動ListView組件的函數 }
procedure TForm1.ListBox1DragOver(Sender, Source: TObject;
X, Y: Integer; State: TDragState; var Accept: Boolean);
begin
{ Scroll Listview when Drag/Drop }
if Y<15 then SendMessage(ListBox1.Handle,WM_VSCROLL,SB_LINEUP,0)
else if ListBox1.Height-Y<15 then SendMessage(ListBox1.Handle,WM_VSCROLL,SB_LINEDOWN,0);
{ whether accept it }
if (Source is TListBox) and (ListBox1.Items.Count>0) and
(ListBox1.ItemAtPos(Point(X,Y),True)<>ListBox1.ItemIndex) then
Accept:=True else Accept:=False;
end; procedure TForm1.ListBox1KeyDown(Sender: TObject;
var Key: Word; Shift: TShiftState);
begin
if Key=46 then { Delete Key }
Button2.OnClick(Sender);
end; procedure TForm1.FormShow(Sender: TObject);
begin
ListBox1.Clear;
LoadIniFile(ListBox1.Items);
end; procedure TForm1.FormCreate(Sender: TObject);
begin
{得到程序路徑}
Application_Path:=ExtractFilePath(ParamStr(0));
end; end.
|
wzpuma
一般會員 發表:10 回覆:19 積分:5 註冊:2003-02-27 發送簡訊給我 |
|
dt520
一般會員 發表:11 回覆:61 積分:14 註冊:2003-05-13 發送簡訊給我 |
|
wnhoo
高階會員 發表:75 回覆:443 積分:198 註冊:2003-04-22 發送簡訊給我 |
//STAT.INI
[System]
VESA=1
ScreenMode=VM_800X600X256
SystemCharSize=16
CurrentGroup=WORD
MainTitle1=ok
MainTitle=1987 [Mouse]
LeftHand=0 [Desktop]
Wallpaper=LOGO256.BMP
TileWallpaper=1 unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,INIFILES; type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure listTitle;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.dfm} procedure TForm1.listTitle;
var filename:string;
i:integer;
breedno:tstringlist; inifile:TINIFILE;
begin
listBox1.Clear;
//取得文件路径
filename:=ExtractFilePath(paramstr(0))+'stat.ini';
inifile:=TInifile.Create(filename);
breedno:=tstringlist.create;
inifile.ReadSection('System',breedno); for i:=0 to breedno.Count-1 do
listbox1.items.add(breedno.Strings[i]);
breedno.Free ;
inifile.Free ;
end; procedure TForm1.Button1Click(Sender: TObject);
begin listTitle;
end; procedure TForm1.Button2Click(Sender: TObject); var
filename:string;
inifile:TINIFILE;
begin
listBox1.Clear;
//取得文件路径
filename:=ExtractFilePath(paramstr(0))+'stat.ini';
inifile:=TInifile.Create(filename);
//删除SYSTEM->MAINTITLE值
inifile.DeleteKey('System','MainTitle');
inifile.UpdateFile ;
inifile.Free ;
listTitle;
end; procedure TForm1.Button3Click(Sender: TObject); var
filename:string;
inifile:TINIFILE;
begin
listBox1.Clear;
//取得文件路径
filename:=ExtractFilePath(paramstr(0))+'stat.ini';
inifile:=TInifile.Create(filename);
//更新值 MainTitle1-》MainTitle2
inifile.DeleteKey('System','MainTitle1');
inifile.WriteString ('System','MainTitle2','ok');
inifile.UpdateFile ;
inifile.Free ;
listTitle;
end; end. 上面实现了显示、删除、更新的操作,供参考! 风花雪月 e梦情缘
發表人 - wnhoo 於 2003/05/16 08:34:33
------
风花雪月 e梦情缘 |
vclphi
一般會員 發表:28 回覆:39 積分:13 註冊:2003-03-06 發送簡訊給我 |
|
wnhoo
高階會員 發表:75 回覆:443 積分:198 註冊:2003-04-22 發送簡訊給我 |
改为2个FORM操作
form1.显示STAT.INI 与显示FORM2的删除、更新操作结果
FORM2 操作删除、更新 unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,INIFILES; type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure listTitle;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject); private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1; implementation uses Unit2; procedure TForm1.listTitle;
var filename:string;
i:integer;
breedno:tstringlist; inifile:TINIFILE;
begin
listBox1.Clear;
//取得文件路径
filename:=ExtractFilePath(paramstr(0)) 'stat.ini';
inifile:=TInifile.Create(filename);
breedno:=tstringlist.create;
inifile.ReadSection('System',breedno); for i:=0 to breedno.Count-1 do
listbox1.items.add(breedno.Strings[i]);
breedno.Free ;
inifile.Free ;
end; procedure TForm1.Button1Click(Sender: TObject);
begin listTitle;
end; procedure TForm1.Button2Click(Sender: TObject);
begin
form2.showmodal;
end; end. unit Unit2; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,INIFILES; type
TForm2 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form2: TForm2; implementation uses Unit1; {$R *.dfm} procedure TForm2.Button1Click(Sender: TObject);
var
filename:string;
inifile:TINIFILE;
begin
form1.listBox1.Clear;
//取得文件路径
filename:=ExtractFilePath(paramstr(0)) 'stat.ini';
inifile:=TInifile.Create(filename);
//删除SYSTEM->MAINTITLE值
inifile.DeleteKey('System','MainTitle');
inifile.UpdateFile ;
inifile.Free ;
Form1.listTitle;
end; procedure TForm2.Button2Click(Sender: TObject); var
filename:string;
inifile:TINIFILE;
begin
Form1.listBox1.Clear;
//取得文件路径
filename:=ExtractFilePath(paramstr(0)) 'stat.ini';
inifile:=TInifile.Create(filename);
//更新值 MainTitle1-》MainTitle2
inifile.DeleteKey('System','MainTitle1');
inifile.WriteString ('System','MainTitle2','ok');
inifile.UpdateFile ;
inifile.Free ;
Form1.listTitle;
end; end.
供参考! 风花雪月 e梦情缘
------
风花雪月 e梦情缘 |
vclphi
一般會員 發表:28 回覆:39 積分:13 註冊:2003-03-06 發送簡訊給我 |
(1)若要求Delete的是ListBox1中选中的项目,而非像
inifile.DeleteKey('System','MainTitle');这样指定的,改如何改。
(2)若要求在Form2中也有ListBox1,删除、更新的操作在Form2.ListBox1中进行,再显示回Form1.ListBox1中,该如何改动代码?
(3)若在Form1中有Edit1,TForm1.Button3Click则Edit1.Text:=关键字的值(如
STAT.INI中[System]的VM_800X600X256)该如何写代码?
我的办法是:
type
datial=record
KEY:string[80];
KEYVAlUE:string[30];
end;
var a:array[1..120] of datial;
procedure TForm1.listTitle;
......
for i:=0 to breedno.Count-1 do
begin
s:=inifile.ReadString('system',breedno.Strings[i],'');
a[i].key:=breedno.Strings[i];
a[i].keyvalue:=s;
list1.items.add(breedno.Strings[i]);
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
edit1.text:=a[list1.ItemIndex].keyvalue;
end;
删除项目用inifile.DeleteKey('system',a[list1.ItemIndex].cTitle);
但我觉得这方法不够好。是否有更好方法?
|
wnhoo
高階會員 發表:75 回覆:443 積分:198 註冊:2003-04-22 發送簡訊給我 |
(1)若要求Delete的是ListBox1中选中的项目,而非像
inifile.DeleteKey('System','MainTitle');这样指定的,改如何改。
(2)若要求在Form2中也有ListBox1,删除、更新的操作在Form2.ListBox1中进行,再显示回Form1.ListBox1中,该如何改动代码?
(3)若在Form1中有Edit1,TForm1.Button3Click则Edit1.Text:=关键字的值(如
STAT.INI中[System]的VM_800X600X256)该如何写代码? 你上面的问题,只要将我给出的方法,变动一下就可以实现的。 至于更新不要删除更新的问题,好象没有什么好的解决方法了! 风花雪月 e梦情缘
------
风花雪月 e梦情缘 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |