toufik 2 Posted September 23, 2022 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
David Hoyle 68 Posted September 23, 2022 The name of your INI files are different... save = my.ini and load = tabibi.ini. Share this post Link to post
Lajos Juhász 293 Posted September 23, 2022 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
toufik 2 Posted September 23, 2022 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
toufik 2 Posted September 23, 2022 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
toufik 2 Posted September 23, 2022 (edited) i really know the fix is in front of my eyes for some reason i'm stuck for two days with this !! Edited September 23, 2022 by toufik Share this post Link to post
David Hoyle 68 Posted September 23, 2022 Besides fixing the INI naming issue are you making sure you are loading the file from the same folder? Share this post Link to post
toufik 2 Posted September 23, 2022 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
David Hoyle 68 Posted September 23, 2022 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
David Hoyle 68 Posted September 23, 2022 Have you checked that the INI file exists with the value you have written? Share this post Link to post
toufik 2 Posted September 23, 2022 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
toufik 2 Posted September 23, 2022 Myinifile := TIniFile.Create('tabibi.ini'); (then the restore code ) to restore from the ini file ,?? Share this post Link to post
Remy Lebeau 1398 Posted September 23, 2022 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
toufik 2 Posted September 23, 2022 (edited) 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 September 23, 2022 by toufik Share this post Link to post
Remy Lebeau 1398 Posted September 26, 2022 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