Jump to content
toufik

save probelm in ini file vcl with tlistbox

Recommended Posts

help need please .............I'm using Delphi 10.4
I'm having a very silly problem restoring i'm trying to store or save tlistbox index that select by the user (ItemIndex) and restore it later on create event 
the save work just fine i think the problem with restore .
save :
var Myinifile := TIniFile; SkinFile := string 

   Myinifile := TIniFile.Create(ExtractFilePath (application.ExeName)+'my.ini');
   Myinifile.WriteString('LastDiskSkin', SkinFile, lst1.ItemIndex.ToString);
restore :

 

  if lst1.ItemIndex < 0 then EXIT;
//     Myinifile := TIniFile.Create('tabibi.ini');
  try
 

 lst1.ItemIndex :=Myinifile.ReadString('LastDiskSkin',SkinFile,'').ToInteger;
 ShowMessage(SkinFile);
  finally
    Myinifile.Free;
  end;

Share this post


Link to post

Why are you using WrieString / ReadString. It would be better:

Myinifile.WriteInteger('LastDiskSkin', SkinFile, lst1.ItemIndex.ToString);

 

lst1.ItemIndex :=Myinifile.ReadInteger('LastDiskSkin',SkinFile, -1); // or some default index.

Share this post


Link to post
Just now, David Hoyle said:

The name of your INI files are different... save = my.ini and load = tabibi.ini.

yeah i did change it s not the problem its not restoring the save index 

Share this post


Link to post
Just now, Lajos Juhász said:

Why are you using WrieString / ReadString. It would be better:

Myinifile.WriteInteger('LastDiskSkin', SkinFile, lst1.ItemIndex.ToString);

 

lst1.ItemIndex :=Myinifile.ReadInteger('LastDiskSkin',SkinFile, -1); // or some default index.

i about two days now trying to fix this and yes i did try with ReadInteger and WriteInteger the problem persist 

Share this post


Link to post

i really know the fix is in front of my eyes for some reason i'm stuck for two days with this !!

Edited by toufik

Share this post


Link to post
Just now, David Hoyle said:

Besides fixing the INI naming issue are  you making sure you are loading the file from the same folder?

well i dont thinks i'm ,,, can you point me in the right direction please

Share this post


Link to post

In your saving code you use the EXE path and my.ini. In your restoration code you do not specify a path just the filename.

Share this post


Link to post
Just now, David Hoyle said:

Have you checked that the INI file exists with the value you have written?

yes the ini file created and index in it just fine in the restore i did not use the ini file at all i think thats problem 

Share this post


Link to post

 Myinifile := TIniFile.Create('tabibi.ini');
(then the restore code )

to restore from the ini file ,??

Share this post


Link to post
5 hours ago, toufik said:

save :
var Myinifile := TIniFile; SkinFile := string 

   Myinifile := TIniFile.Create(ExtractFilePath (application.ExeName)+'my.ini');
   Myinifile.WriteString('LastDiskSkin', SkinFile, lst1.ItemIndex.ToString);

First, do not store your INI file in the same folder as your EXE. If the EXE is installed in a protected folder, like under %ProgramFiles%, then this will not work.  You should instead save the INI file in a subfolder you create for your app under the user's %APPDATA% folder, for instance.

 

Second, use (Write|Read)Integer() instead of (Write|Read)String().

 

Third, what is the value of SkinFile?  And did you verify that the restoration code is using the same value?

5 hours ago, toufik said:

restore :

 

  if lst1.ItemIndex < 0 then EXIT;
//     Myinifile := TIniFile.Create('tabibi.ini');
  try
 

 lst1.ItemIndex :=Myinifile.ReadString('LastDiskSkin',SkinFile,'').ToInteger;

You are not loading from the same file that you saved to.  Also, why are you checking the current ItemIndex value before loading the value from the INI file?

 

With that said, try something more like this:

 

uses
  ..., SysUtils, ComObj, SHFolder;
  
procedure TMyForm.GetINIFilePath(CanCreate: Boolean): string;
var
  AppDataPath: array[0..MAX_PATH] of Char;
begin
  OleCheck(SHGetFolderPath(Handle, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, AppDataPath));
  Result := IncludeTrailingPathDelimiter(AppDataPath) + 'MyApp';
  if CanCreate then
    ForceDirectories(Result);
  Result := IncludeTrailingPathDelimiter(Result) + 'my.ini';
end;

procedure TMyForm.SaveINI;
var
  Myinifile: TIniFile;
  SkinFile: string;
begin
  SkinFile := ...;
  Myinifile := TIniFile.Create(GetINIFilePath(True));
  try
    Myinifile.WriteInteger('LastDiskSkin', SkinFile, lst1.ItemIndex);
    ...
  finally
    Myinifile.Free;
  end;
end;

procedure TMyForm.LoadINI;
var
  Myinifile: TIniFile;
begin
  SkinFile := ...;
  Myinifile := TIniFile.Create(GetINIFilePath(False));
  try
    lst1.ItemIndex := Myinifile.ReadInteger('LastDiskSkin', SkinFile, -1);
    ...
  finally
    Myinifile.Free;
  end;
end;

procedure TMyForm.FormCreate(Sender: TObject);
begin
  LoadINI;
end;

procedure TMyForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveINI;
end;

 

Share this post


Link to post
3 hours ago, Remy Lebeau said:

First, do not store your INI file in the same folder as your EXE. If the EXE is installed in a protected folder, like under %ProgramFiles%, then this will not work.  You should instead save the INI file in a subfolder you create for your app under the user's %APPDATA% folder, for instance.

 

Second, use (Write|Read)Integer() instead of (Write|Read)String().

 

Third, what is the value of SkinFile?  And did you verify that the restoration code is using the same value?

You are not loading from the same file that you saved to.  Also, why are you checking the current ItemIndex value before loading the value from the INI file?

 

With that said, try something more like this:

 


uses
  ..., SysUtils, ComObj, SHFolder;
  
procedure TMyForm.GetINIFilePath(CanCreate: Boolean): string;
var
  AppDataPath: array[0..MAX_PATH] of Char;
begin
  OleCheck(SHGetFolderPath(Handle, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, AppDataPath));
  Result := IncludeTrailingPathDelimiter(AppDataPath) + 'MyApp';
  if CanCreate then
    ForceDirectories(Result);
  Result := IncludeTrailingPathDelimiter(Result) + 'my.ini';
end;

procedure TMyForm.SaveINI;
var
  Myinifile: TIniFile;
  SkinFile: string;
begin
  SkinFile := ...;
  Myinifile := TIniFile.Create(GetINIFilePath(True));
  try
    Myinifile.WriteInteger('LastDiskSkin', SkinFile, lst1.ItemIndex);
    ...
  finally
    Myinifile.Free;
  end;
end;

procedure TMyForm.LoadINI;
var
  Myinifile: TIniFile;
begin
  SkinFile := ...;
  Myinifile := TIniFile.Create(GetINIFilePath(False));
  try
    lst1.ItemIndex := Myinifile.ReadInteger('LastDiskSkin', SkinFile, -1);
    ...
  finally
    Myinifile.Free;
  end;
end;

procedure TMyForm.FormCreate(Sender: TObject);
begin
  LoadINI;
end;

procedure TMyForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SaveINI;
end;

 

worked great, with some tweaks 
i replaced ,procedure with function 
and handle in line 7 with HWND_DESKTOP for some reason i did get an error with it  
(([dcc32 Error] Unit1.pas(192): E2003 Undeclared identifier: 'Handle'))
thank you and everyone else who tried to help 
by the way big fan of your work here and in every other platform,, great job,, take care everyone and have a nice day or a night ....

Edited by toufik

Share this post


Link to post
On 9/23/2022 at 2:38 PM, toufik said:

i replaced ,procedure with function

Yup, minor typo on my part.

On 9/23/2022 at 2:38 PM, toufik said:

and handle in line 7 with HWND_DESKTOP for some reason i did get an error with it  
(([dcc32 Error] Unit1.pas(192): E2003 Undeclared identifier: 'Handle'))

You should not have gotten that error.  As you can see in my example, the function is a method of the Form class, so it would be using the Form's Handle.

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×