> Is there a way to save easily the strings of CheckListBox + the
> states (checked or not) ? And a way to load them and automatically
> restore the states for each string ?
TCheckListBox does not expose access to that functionality. You will have
to do it manually.
Gambit
--
Olivier Pons
http://olivier.pons.free.fr/
Yes. There are a few easy ways. That is, none of the solutions that
you'll have to write yourself should be particularly challenging.
> And a way to load them and automatically restore the states for each
> string ?
No. Nothing automatic.
--
Rob
For some reason I can't find the CheckListBox component, but if the items in
it and it's state are public properties then Orpheus should be able to save
and restore it's state for you, all you do is add the properties to be
saved. But from what I've seen by the other responses it would look like
they are either not properties, or not Public. Well, you might as well go
ahead and check anyways.
- Dan
> For some reason I can't find the CheckListBox component
> if the items in it and it's state are public properties then Orpheus
> should be able to save and restore it's state for you
The Items property is a published TStrings, but the Checked[] property is
public instead. Assuming Orpheus uses RTTI to do its work, then it won't be
able to stream non-published properties.
Gambit
> Is there a way to save easily the strings of CheckListBox + the states (checked or not) ?
> And a way to load them and automatically restore the states for each string ?
The following (untested) CLASS HELPER should allow you to do it
directly on the CheckListBox variable. If you do not run at least
Delphi 2006, then you should be able to figure out how to do it
manually by looking at the code. Put the code below into a UNIT and
USE it in the form that contains the CheckListBox. Then just use
CheckListBox1.SaveStatesToFile('C:\STATES.DAT') to save the items +
their respective states to the file 'C:\STATES.DAT'.
TYPE
TCheckListBoxHelper = CLASS HELPER FOR TCheckListBox
PROCEDURE SaveStatesToStream(S : TStream);
PROCEDURE LoadStatesFromStream(S : TStream);
PROCEDURE SaveStatesToFile(CONST FileName :
STRING);
PROCEDURE LoadStatesFromFile(CONST
FileName : STRING);
END;
PROCEDURE TCheckListBoxHelper.SaveStatesToStream(S : TStream);
VAR
TXT : AnsiString;
I : INTEGER;
BEGIN
FOR I:=0 TO Items.Count-1 DO BEGIN
IF Checked[I] THEN TXT:='+' ELSE TXT:='-';
TXT:=TXT+Items[I]+#13#10;
S.Write(TXT[1],LENGTH(TXT))
END
END;
PROCEDURE TCheckListBoxHelper.LoadStatesFromStream(S : TStream);
VAR
TXT : AnsiString;
C : AnsiChar;
I : INTEGER;
CHK : BOOLEAN;
BEGIN
Items.Clear;
WHILE S.Position<S.Size DO BEGIN
TXT:='';
REPEAT
S.Read(C,SizeOf(C));
IF C=#13 THEN BEGIN
S.Read(C,SizeOf(C)); // Skip past LF character //
BREAK
END;
TXT:=TXT+C
UNTIL S.Position>=S.Size;
CHK:=(TXT[1]='+');
DELETE(TXT,1,1);
Checked[Items.Add(TXT)]:=CHK
END
END;
PROCEDURE TCheckListBoxHelper.SaveStatesToFile(CONST FileName :
STRING);
VAR
S : TStream;
BEGIN
S:=TFileStream.Create(FileName,fmCreate);
TRY
SaveStatesToStream(S)
FINALLY
FreeAndNIL(S)
END
END;
PROCEDURE TCheckListBoxHelper.LoadStatesFromFile(CONST FileName :
STRING);
VAR
S : TStream;
BEGIN
S:=TFileStream.Create(FileName,fmOpenRead);
TRY
LoadStatesFromStream(S)
FINALLY
FreeAndNIL(S)
END
END;
That's what I thought so I was just wondering if someone has ever done such thing.
Anyway I'll do it on my own... sad.