从 checklistbox 到 tcxchecklistbox(将状态保存到 TMemoryStream)

From checklistbox to tcxchecklistbox (save states to TMemoryStream)

任何人都可以帮助我如何将其转换为与 tcxchecklistbox 一起使用?

我的保存过程看起来像...

  procedure Tfrm_A.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 Tfrm_A.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;

我的问题是

buf := CheckListBox.Items.Text; 

TcxChecklistbox 有 checklistbox.items[Index].text属性

感谢您的帮助!

您可以使用 TStringStream 来完成此操作。基本上,这只是迭代 cxCheckBoxList Items 并将一个字符写入 StringStream 以指示复选框是否被选中,然后一次从流中读回一个字符的问题。

function StateToString(Checked : Boolean) : String;
begin
  if Checked then
    Result := '+'
  else
    Result := '-';
end;

procedure TForm1.SaveStatesToStream(SS : TStringStream);
var
  i : integer;
begin
  SS.Clear;
  SS.Position := 0;
  for i := 0 to cxCheckListBox1.Items.Count - 1 do begin
    SS.WriteString(StateToString(cxCheckListBox1.Items[i].Checked));
  end;
  Memo1.Lines.Add('>' + SS.DataString + '<');
end;

procedure TForm1.LoadStatesFromStream(SS : TStringStream);
var
  i : integer;
  S : String;
begin
  CheckBoxList.ClearCheckmarks;
  SS.Position := 0;
  i := 0;
  while (i <= cxCheckListBox1.Items.Count - 1) and (SS.Position < SS.Size) do begin
    S := SS.ReadString(1);
    cxCheckListBox1.Items[i].Checked := S = '+';
    Inc(i);
  end;
end;

在Delphi西雅图测试