Reputation: 2416
I want to save to .ini and after fill checklistbox items from the saved ini file. I have 2 listboxes like...
First listbox contains tables:
The second listbox links to the first, if I click on Cars table and checked it the following datas will be visible on the second checklistbox...
Second listbox contains table fields:
CARS
- Car_ID
- Car_Name
- Car_LicNum
- Car_Color etc..
USERS
Anyone can help me how can I save all the checked items (from checklistbox 1 and checklistbox2) to .ini file? And after how can I load and fill the checklistboxes with them?
I did for the first checklistbox but...
procedure TForm1.btn_SaveClick(Sender: TObject);
begin
ini := TIniFile.Create('C:\checklistbox.ini');
try
for i := 0 to Checklistbox1.Items.Count - 1 do
ini.WriteBool('items', Checklistbox1.Items[i], Checklistbox1.Checked[i]);
finally
ini.Free;
end;
end;
Loading items to checklistbox1
procedure TForm1.btn_LoadClick(Sender: TObject);
begin
ini := TIniFile.Create('c:\checklistbox.ini');
try
ini.ReadSection('items', Checklistbox1.Items);
for i := 0 to Checklistbox1.Items.Count - 1 do
CheckListbox1.Checked[i] := ini.ReadBool('items', Checklistbox1.Items[i], False);
finally
ini.Free;
end;
end;
I dont know how can I save items from checklistbox2 which items links to checklistbox1 items. I want to load all the checked items after. I am using Delphi XE7 at the moment. Thanks for the answers!
Upvotes: 0
Views: 2222
Reputation: 6013
I guess your problem is getting your head around the fact that the contents of the second list box change, and so the risk of error is quite high. I agree with that and so the answer is to ignore the list boxes themselves and focus on what they represent, and so store the data that the user wants to see - and in this case I would use field names for that - so
ini.WriteString('File To View', 'Name', 'Cars');
and for the fields
ini.WriteInteger('Cars', 'Count', 2);
ini.WriteString('Cars', 'Field 1', 'Cars_ID');
ini.WriteString('Cars', 'Field 2', 'Car_LICNUM');
I guess that you only allow one box in the first checkbox to be checked. If that were not true, or later became not true, you would add count and 'Name x' parameters like this
ini.WriteInteger('File To View', 'Count', 2);
ini.WriteString('File To View', 'Name 1', 'Cars');
ini.WriteString('File To View', 'Name 2', 'Users');
So changing your GUI later becomes easy, as does making your new program backwards compatible. This is the point LU RD makes about basing your INI file on your business model and not your GUI.
Note also the fact that you may store multiple sections - one for each file in fact, but that doesn't really matter and has the hidden benefit that the INI file 'remembers' the users last choice of fields for each file.
Upvotes: 0