not a loof
not a loof

Reputation: 29

How to save to an INI file delphi

I have buttons which when clicked store their caption in a TStringList

ChairList : TStringList;

procedure TForm1.FormCreate(Sender: TObject);
begin
ChairList := TStringList.Create;
end;

An example of one button is:

procedure TForm1.Table17Click(Sender: TObject);
begin
Label1.Visible := false;
if (BottomPanel.Visible = false) then
begin
  Label1.Visible := true;

  LastClicked := 'Table 17';
  ChairList.Add(LastClicked);

But when I read the file I get no result back. I tested it by using a ShowMessage just to see if anything would be read and I just get a blank ShowMessage

The following code is the save procedure:

Procedure TForm1.SaveToFile(Const Filename : String);
Var
INI : TMemIniFile;

Procedure SaveReserve();
var
section : String;

Begin
  Section := 'Table';
  ini.writeString(Section, 'LastClickedID', LastClicked);
End;

begin
Ini := Tmeminifile.Create(filename);
ini.Clear;
try
SaveReserve();
Ini.UpdateFile;
finally
Ini.Free;
end;
end;

Then the next Procedure is the load file:

Procedure TForm1.LoadFile(const Filename : String);
var
INI : TMemInifile;
Sections : TStringList;
i : Integer;
LastClickedID : String;
Procedure LoadChair(Const Section: String);
Begin
  LastClickedID := INI.ReadString(Section, 'LastClickedID', LastClicked)
End;

Begin
ChairList.Clear;

INI := TMEMINIFILE.Create(Filename);
 Try
 Sections := TStringList.Create;
 Try
   Ini.ReadSections(Sections);
   for i := 0 to (Sections.Count - 1) do
   Begin
     if startstext('LastClickedID', Sections[I]) then
      begin
        LastClickedID := (copy(Sections[I], 12, MaxInt));

      end;
   End;
 Finally
  Sections.Free;
 End;
 Finally
 INI.Free;
end;
ShowTable(LastClickedID);
end;

ShowTable(LastClickedID); is just the part where i test it

How can i make it so it saves the caption and then when i load it up it shows any table caption saved? I only need it to save the caption of an object selected. When the user selects a button it adds the caption to the stringlist which is then read to the ini file

Upvotes: 2

Views: 9454

Answers (4)

Remy Lebeau
Remy Lebeau

Reputation: 597941

Your code fails to load the value back because you are trying to read it from the wrong area of the INI file.

You are writing the value to the 'LastClickedID' entry of the 'Table' section, which produces this INI data:

[Table]
LastClickedID=Value

But then you are trying to read the value from the 'LastClickedID' entry of any section that begins with the 'LastClickedID' prefix, not from the 'Table' section that you previously wrote to. That would only work if the INI data looked more like this instead:

[LastClickedID]
LastClickedID=Value

Since that section does not exist in the INI, that is why your LastClickedID variable is always blank (if LastClicked is also blank to begin with).

You don't need to use Ini.ReadSections() in this example. Just use Ini.ReadString() by itself with the proper 'Table' section name, eg:

procedure TForm1.LoadFile(const Filename : String);
var
  Ini : TMemIniFile;
  LastClickedID: string;

  procedure LoadReserve;
  var
    section : String;
  begin
    Section := 'Table';
    LastClickedID := Ini.ReadString(Section, 'LastClickedID', LastClicked);
  end;

begin
  Ini := TMemIniFile.Create(Filename);
  try
    LoadReserve;
  finally
    Ini.Free;
  end;

  ShowTable(LastClickedID);
end;

In general, you should be mirroring the opposite of whatever your SaveToFile() code is doing.

Upvotes: 2

The Bitman
The Bitman

Reputation: 1329

You can create a general ini file utility object, which can read/write under the predefined key, the properties collected by a dictionary object. This is a flexible, reusable solution. You can collect the clicked control captions into the dictionary and save the list at the end of your process. You should use a dictionary because a key/value pair needed for an ini file operation.

uses
    Generics.Collections
  , System.IniFiles
  ;

TIniFileUtility = class
  public
    class read( ini_ : TIniFile; keyName_ : string; props_ : TDictionary<string,string> );
    class write( ini_ : TIniFile; keyName_ : string; props_ : TDictionary<string,string> );
end;



class procedure TIniFileUtility.write( ini_ : TIniFile; keyName_ : string; props_ : TDictionary<string,string>);
var
  i : integer;
  key, value : string;
begin
  //... some input parameter checking
    for key in props_.keys do
    begin
      value := props_.Items[key];
      ini_.writeString( keyName_, key, value );
    end;
end;

procedure caller(Sender: TObject);
var
  ini : TIniFile;
  props : TDictionary<string,string>;
begin
  ini := TIniFile.create( 'c:\temp\defaults.ini' );
  try
    props := TDictionary<string,string>.Create;
    try
      props.Add( 'key1', 'stringValue' );
      props.Add( 'key2', intToStr( 2 ) );
      TIniFileUtility.write( ini, '\x\y\z\', props );
    finally
      props.Free;
      props := NIL;
    end;
  finally
    ini.Free;
    ini := NIL;
  end;
end;

Upvotes: 0

Beno&#238;t Blanc
Beno&#238;t Blanc

Reputation: 301

I advise you to read this documentation and example to use TMemIniFile and TIniFile. It should be helpful...

Here is my code :

Procedure TForm1.SaveToFile(Const Filename : String);
Var
  Ini : TIniFile;    
begin
  Ini := TIniFile.Create(filename);      
  try
    Ini.WriteString('Table', 'LastClickedID', LastClicked);
  finally
    Ini.Free;
  end;
end;

Procedure TForm1.LoadFile(const Filename : String);
var
  Ini : TInifile;
  LastClickedID : String;    
begin    
  Ini := TIniFile.Create(Filename);
  try
    LastClickedID := Ini.ReadString('Table', 'LastClickedID', '');
  finally
    Ini.Free;
  end;
  ShowTable(LastClickedID);
end;

Upvotes: 0

Vlad Ivchenko
Vlad Ivchenko

Reputation: 562

try this code! Procedure for write ini:

procedure write_ini;
var
  ini:TIniFile;
begin
  ini:=TIniFile.Create('MainForm.ini');
  ini.WriteInteger('FORM', 'Top', MainForm.Top);
  ini.WriteInteger('FORM', 'Left', MainForm.Left);
  ini.WriteInteger('FORM', 'Width', MainForm.Width);
  ini.WriteInteger('FORM', 'Height', MainForm.Height);
  ini.WriteString('USER', 'Name', MainForm.userName.Text);
  ini.WriteInteger('USER','Pol', MainForm.Combo.ItemIndex);
  ini.Free;
end;

Procedure for read ini:

 procedure read_ini;
    var
     ini:TIniFile;
    begin
      ini:=TiniFile.Create('MainForm.ini');
      MainForm.Top:=ini.ReadInteger('FORM', 'Top', 0);
      MainForm.Left:=ini.ReadInteger('FORM', 'Left', 0);
      MainForm.Width:=ini.ReadInteger('FORM', 'Width', 226);
      MainForm.Height:=ini.ReadInteger('FORM', 'Height', 123);
      MainForm.userName.Text:=ini.ReadString('USER', 'Name', 'Anonim');
      MainForm.Combo.ItemIndex:=ini.ReadInteger('USER', 'Pol', 1);
      ini.Free;
    end;

Upvotes: 5

Related Questions