Board index » delphi » TCheckListBox' s checked status into TMemoryStream

TCheckListBox' s checked status into TMemoryStream

I need help with TMemoryStreams. I want to save TCheckListBox's data with
checked status into TMemoryStream than I want to reload from TMemoryStream
to TCheckListBox.
I have got code under this message but I cannot use with Checked status
where is my problem?
How can I fix this code' s problem? Thanx...

procedure TfrmReports.SaveCheckListBoxData(S:TMemoryStream; CheckListBox:
TCheckListBox);
var
 i: integer;
 b: boolean;
begin
 S.Clear;
 CheckListBox.Items.SaveToStream(S);

 for i:= 0 to Pred(CheckListBox.Items.Count) do
  begin
   b:= CheckListBox.Checked[i];
   s.Write(b,1);
  end;
end;

procedure TFrmReports.LoadCheckListBoxData(S:TMemoryStream; CheckListBox:
TCheckListBox);
var
 i: integer;
 b: Boolean;
begin
 S.Position := 0;
 CheckListBox.Items.LoadFromStream(S);

 for i:= 0 to Pred(CheckListBox.Items.Count) do
 begin
  s.Read(b,1);
  CheckListBox.Checked[i] := b;
 end;
end;

Then I am using this code like that;

procedure TfrmTest.BitBtn5Click(Sender: TObject);
var
  s1: TMemoryStream;
begin
  s1:= TMemoryStream.Create;
  SaveCheckListBoxData(S1,CheckListBox1);
  LoadCheckListBoxData(S1,CheckListBox2);
end;

 

Re:TCheckListBox' s checked status into TMemoryStream


The stars so gaily glistened...
...while the fading voice of Murat BUDAK whispered through the darkness...

Take a look at classes.pas, methods (and always use VCL Sources)
 TStrings.LoadFromStream and TStrings.SaveToStream

You will see, that the stored thing is TStrings.Text - but without it's
length!!!
Instead, LoadFromStream assumes size to be Stream.Size - Stream.Position;

All you stored bools are treated as the end of the last line.

So You may try  a lot of workarounds: Here are the 1st 3 i got in my mind :

=========Beginning of the citation==============
procedure TfrmReports.SaveCheckListBoxData(S:TMemoryStream; CheckListBox:
TCheckListBox);
var
 i: integer;
 b: boolean;
begin
 S.Clear;

 for i:= 0 to Pred(CheckListBox.Items.Count) do
  begin
   b:= CheckListBox.Checked[i];
   s.Write(b,1);
  end;

 CheckListBox.Items.SaveToStream(S);
end;

procedure TFrmReports.LoadCheckListBoxData(S:TMemoryStream; CheckListBox:
TCheckListBox);
var
 i: integer;
 b: Boolean; ch: char;
begin
With S do begin
  Position := 0;
   repeat  Read(ch,1); until ch>#1;
  Position := Position - 1;
end;

 CheckListBox.Items.LoadFromStream(S);

Position := 0;
 for i:= 0 to Pred(CheckListBox.Items.Count) do
 begin
  s.Read(b,1);
  CheckListBox.Checked[i] := b;
 end;
end;

=========The end of the citation================
=========Beginning of the citation==============
procedure TfrmReports.SaveCheckListBoxData(S:TMemoryStream; CheckListBox:
TCheckListBox);
var
 i: integer;  b: boolean; sl :TStrings;
begin
 S.Clear;
 sl:=TStringList.Create;
 try

sl.Assign(CheckListBox.Items);

 for i:= 0 to Pred(CheckListBox.Items.Count) do
  begin
   b:= CheckListBox.Checked[i];
   sl[i]:=char(ord('0')+byte(b)) + sl[i]; // i hate using #0 :-)
  end;

sl.SaveToStream(S);
finally sl.Free; end;
end;

procedure TFrmReports.LoadCheckListBoxData(S:TMemoryStream; CheckListBox:
TCheckListBox);
var
 i: integer; b: Boolean; sl: TStrings;
begin
 S.Position := 0;

 sl:=TStringList.Create;
 try
 sl.LoadFromStream(S);
CheckListBox.Items.BeginUpdate;
CheckListBox.Items.Clear;

 for i:= 0 to Pred(sl.Count) do
 begin
  CheckListBox.Items.Add(Copy(sl[i],2,MaxInt));
  CheckListBox.Checked[i] := sl[i][1]<>'0';
 end;
finally sl.Free; CheckListBox.Items.EndUpdate; end; end;
=========The end of the citation================
=========Beginning of the citation==============
procedure TfrmReports.SaveCheckListBoxData(S:TMemoryStream; CheckListBox:
TCheckListBox);
var
 i: integer;  b: boolean; sl :TStrings; ck:string;
begin
 S.Clear;
 sl:=TStringList.Create;
 try

  sl.Assign(CheckListBox.Items);
 SetLength(ck,sl.Count);

 for i:= 0 to Pred(CheckListBox.Items.Count) do
  begin
   b:= CheckListBox.Checked[i];
   ck[i+1]:=char(ord('0')+byte(b)) + sl[i]; // i hate using #0 :-)
  end;
  sl.Insert(0,ck);
sl.SaveToStream(S);
finally sl.Free; end;
end;

procedure TFrmReports.LoadCheckListBoxData(S:TMemoryStream; CheckListBox:
TCheckListBox);
var
 i: integer; b: Boolean; sl: TStrings; ck:string;
begin
 S.Position := 0;

 sl:=TStringList.Create;
 try
 sl.LoadFromStream(S);
ck:=sl[0]; sl.Delete(0);

CheckListBox.Items.Assign(sl);

 for i:= 0 to Pred(sl.Count) do
  CheckListBox.Checked[i] := ck[i+1]<>'0';

finally sl.Free; end; end;
=========The end of the citation================

Mail me: the_Arioch.nm.ru   ICQ: ask    WinAmp: [ too serious to play ]
Garbage - Temptation Waits

Re:TCheckListBox' s checked status into TMemoryStream


I liked your Poem.
Thanks Arioch. Chris Willig helped me from objectpascal newsgroup.
and you are right. I think my fault is TCheckListBox.LoadFromStream :o(

They send to me that code;
it's worked perfectly.

procedure TfrmReports.SaveCheckListBoxData(S:TMemoryStream;
CheckListBox:
TCheckListBox);
var
 i: longint;
 b: boolean;
 buf : string;
begin
 S.Clear;
 buf := CheckListBox.Items.Text;
 i := Length(buf);
 S.Write(i, SizeOf(i));
 if i > 0 then begin
   S.Write(buf[1], i);

   for i:= 0 to Pred(CheckListBox.Items.Count) do
    begin
     b:= CheckListBox.Checked[i];
     s.Write(b,1);
    end;
 end;
end;

procedure TFrmReports.LoadCheckListBoxData(S:TMemoryStream;
CheckListBox:
TCheckListBox);
var
 i: longint;
 b: Boolean;
 buf : string;
begin
 S.Position := 0;
 S.Read(i, SizeOf(i));
 if i > 0 then begin
   SetLength(buf, i);
   S.Read(buf[1], i);
   CheckListBox.Items.Text := buf;

   for i:= 0 to Pred(CheckListBox.Items.Count) do
   begin
    s.Read(b,1);
    CheckListBox.Checked[i] := b;
   end;
 end;
end;

Other Threads