关于读取INI文件的问题,控件Combobox1的Items里我设置了三个数5、8、10,如果我选择了5,那么下次运行的时候Combobox1里显示的是5;如果我选择了8,那么下次运行的时候显示的还是8;如果我选择了10,那么下次运行显示的还是10;我的代码如下:关键字的值怎么设置?谢谢高手指教!
procedure TFrmsys.FormCreate(Sender: TObject);
var
filename:string;
begin
filename:=ExtractFilePath(paramstr(0))+'myini.ini';
myinifile:=TInifile.Create(filename);
combobox1.Text:=myinifile.ReadString('newini',‘信息提示时间’,???);
end;procedure TFrmsys.FormDestroy(Sender: TObject);
begin
myinifile.writestring('newini',‘信息提示时间’,combobox1.Text);
myinifile.Free;
end;
procedure TFrmsys.FormCreate(Sender: TObject);
var
filename:string;
begin
filename:=ExtractFilePath(paramstr(0))+'myini.ini';
myinifile:=TInifile.Create(filename);
combobox1.Text:=myinifile.ReadString('newini',‘信息提示时间’,???);
end;procedure TFrmsys.FormDestroy(Sender: TObject);
begin
myinifile.writestring('newini',‘信息提示时间’,combobox1.Text);
myinifile.Free;
end;
[newini]
信息提示时间=5类似这样内容
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,IniFiles, StdCtrls;type
TForm1 = class(TForm)
ComboBox1: TComboBox;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
myinifile:TIniFile;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
var
filename:string;
begin
filename:=ExtractFilePath(paramstr(0))+'myini.ini';
myinifile:=TInifile.Create(filename);
combobox1.Text:=myinifile.ReadString('newini','信息提示时间','');
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
myinifile.writestring('newini','信息提示时间',Combobox1.Text);
myinifile.Free;
end;end.
你在procedure TFrmsys.FormDestroy(Sender: TObject);里面加一句ShowMessage('ok;');看看窗体销毁时有没有将信息写入ini文件里面。
begin
myinifile.writestring('newini','信息提示时间',Combobox1.Text);
myinifile.Free;
end;
这句改成 combobox1.ItemIndex := combobox1.Items.IndexOf(myinifile.ReadString('newini',‘信息提示时间’,???));
var
ms:TMemoryStream;
begin
if FileExists('e:\a.ck') Then
begin
Try
Ms:=TMemoryStream.Create;
Ms.LoadFromFile('e:\a.ck');
ComboBox1:=TComboBox(Ms.ReadComponent(ComboBox1));
finally
FreeAndNil(Ms);
end;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
ms:TMemoryStream;
begin
Try
Ms:=TMemoryStream.Create;
Ms.WriteComponent(ComBoBox1);
Ms.SaveToFile('e:\a.ck');
finally
FreeAndNil(Ms);
end;
end;end.