Title: TCheckListBox LoadFromFile/SaveToFile Method, included checked state?
Question: What to use the TCheckListBox LoadFromFile and SaveToFile Method, and store the checked state at the same time?
Answer:
What to use the TCheckListBox LoadFromFile and SaveToFile Method, and store the checked state at the same time?
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.
{====================================}
Procedure TFrameRuleEngine.SaveRules;
{====================================}
Var
i: Integer;
begin
i := 0;
While i Begin
If CheckListBoxRule.Items[i] = '' Then
Begin
// Delete entry it is empty
CheckListBoxRule.Items.Delete(i);
End
Else
Begin
// Add a 1 or 0 as the first charter in the entry for checked or not checked
CheckListBoxRule.Items[i] := IntToStr(Integer(CheckListBoxRule.Checked[i])) + CheckListBoxRule.Items[i];
Inc(i);
End;
End;
// Save the full list as normal
CheckListBoxRule.Items.SaveToFile(ExtractFilePath(Application.ExeName) + 'Rule.Txt');
end;
{===================================}
Procedure TFrameRuleEngine.LoadRules;
{===================================}
Var
sChecked: String;
i: Integer;
begin
If FileExists(ExtractFilePath(Application.ExeName) + 'Rule.Txt') Then
Begin
// Read the file as normal
CheckListBoxRule.Items.LoadFromFile(ExtractFilePath(Application.ExeName) + 'Rule.Txt');
i := 0;
While i Begin
If CheckListBoxRule.Items[i] = '' Then
Begin
// Delete an empty entry
CheckListBoxRule.Items.Delete(i);
End
Else
Begin
// Get the checked state
sChecked := Copy(CheckListBoxRule.Items[i], 1, 1);
CheckListBoxRule.Items[i] := Copy(CheckListBoxRule.Items[i], 2, Length(CheckListBoxRule.Items[i]));
// Update the Checked property
CheckListBoxRule.Checked[i] := Boolean(StrToInt(sChecked));
Inc(i);
End;
End;
End;
end;
Bjarne \v/
http://www.go2NTS.com