Author: Bjarne Winkler
What to use the TCheckListBox LoadFromFile and SaveToFile Method, and store the
checked state at the same time?
Answer:
If you embed the Checked property into the actual entry as a “1” or “0” charter you
can save the file with the normal SaveToFile method. When a file is loaded using
the LoadFromFile method as normal. Then extract the first charter from the entry
and you will have the checked state.
1 2 {====================================}3 4 procedure TFrameRuleEngine.SaveRules;
5 {====================================}6 var7 i: Integer;
8 9 begin10 i := 0;
11 while i < CheckListBoxRule.Items.Count do12 begin13 if CheckListBoxRule.Items[i] = '' then14 begin15 // Delete entry it is empty16 CheckListBoxRule.Items.Delete(i);
17 end18 else19 begin20 // Add a 1 or 0 as the first charter in the entry for checked or not checked21 CheckListBoxRule.Items[i] := IntToStr(Integer(CheckListBoxRule.Checked[i])) +
22 CheckListBoxRule.Items[i];
23 Inc(i);
24 end;
25 end;
26 // Save the full list as normal27 CheckListBoxRule.Items.SaveToFile(ExtractFilePath(Application.ExeName) +
28 'Rule.Txt');
29 end;
30 31 {===================================}32 33 procedure TFrameRuleEngine.LoadRules;
34 {===================================}35 var36 sChecked: string;
37 i: Integer;
38 39 begin40 if FileExists(ExtractFilePath(Application.ExeName) + 'Rule.Txt') then41 begin42 // Read the file as normal43 CheckListBoxRule.Items.LoadFromFile(ExtractFilePath(Application.ExeName) +
44 'Rule.Txt');
45 i := 0;
46 while i < CheckListBoxRule.Items.Count do47 begin48 if CheckListBoxRule.Items[i] = '' then49 begin50 // Delete an empty entry51 CheckListBoxRule.Items.Delete(i);
52 end53 else54 begin55 // Get the checked state56 sChecked := Copy(CheckListBoxRule.Items[i], 1, 1);
57 CheckListBoxRule.Items[i] := Copy(CheckListBoxRule.Items[i], 2,
58 Length(CheckListBoxRule.Items[i]));
59 // Update the Checked property60 CheckListBoxRule.Checked[i] := Boolean(StrToInt(sChecked));
61 Inc(i);
62 end;
63 end;
64 end;
65 end;