关于读取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;

解决方案 »

  1.   

    问号那个地方用空'' 就可以了。意思是当读取myini.ini文件里[newini]的‘信息提示时间’值不存在,空为默认值。
      

  2.   

    你运行一次之后,看看程序的文件夹下有没有myini.ini文件,里面有没有
    [newini]
    信息提示时间=5类似这样内容
      

  3.   

    myini.ini文件里是空的,没有任何内容。
      

  4.   

    楼主为什么必须用ini解决这种问题呢?
      

  5.   

    我用你的代码是这样测试的:unit Unit1;interfaceuses
      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文件里面。
      

  6.   

    我这里是可以的,XP Delphi7。难到你的delphi有问题?
      

  7.   

    我的也是XP Delphi7,555555555555555~~~~怎么办啊,要不我把这页的代码都给你发过去吧,行吗
      

  8.   

    在写Ini的代码写到Form的OnClose中试试,在OnDestroy中有时候是有问题,我碰到过procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
       myinifile.writestring('newini','信息提示时间',Combobox1.Text);
       myinifile.Free;
    end;
      

  9.   

    感觉楼主好像是 combobox 是只能下拉选一个值, 而不能由用户输入, 要不然早对了combobox1.Text:=myinifile.ReadString('newini',‘信息提示时间’,???);
    这句改成 combobox1.ItemIndex := combobox1.Items.IndexOf(myinifile.ReadString('newini',‘信息提示时间’,???));
      

  10.   

    现学现卖代码如下:procedure TForm1.FormCreate(Sender: TObject);
    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.
      

  11.   

    谢谢postren(小虫),放在OnClose中果然OK了