【问题标题】:From checklistbox to tcxchecklistbox (save states to TMemoryStream)从 checklistbox 到 tcxchecklistbox(将状态保存到 TMemoryStream)
【发布时间】:2016-02-22 10:51:37
【问题描述】:

任何人都可以帮助我如何将其转换为与 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].textproperty

感谢您的帮助!

【问题讨论】:

    标签: delphi devexpress delphi-xe


    【解决方案1】:

    您可以使用TStringStream 来执行此操作。基本上,这只是一个迭代cxCheckBoxListItems并将一个字符写入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;
    

    在西雅图德尔福测试

    【讨论】:

      猜你喜欢
      • 2016-06-04
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      • 2018-07-14
      • 2021-09-28
      • 2022-10-20
      • 2020-07-29
      • 2016-09-15
      相关资源
      最近更新 更多