Search code examples
delphidevexpressdelphi-xedelphi-xe7

From TcxCheckListBox to TcxCheckGroupBox (Load States from TStringStream)


Anyone can help how can I transfrom this to work with TcxCheckGroup? My procedure can be load checked Items states to cxCheckListBox.

Working example with TcxCheckListBox...

procedure Tfrm.LoadStatesFromStream(SS: TStringStream);
var
  i : integer;
  S2 : String;
begin

  SS.Position := 0;
  i := 0;
while (i <= cxCheckListBox1.Items.Count - 1) and (SS.Position < SS.Size) do    
begin
  S2 := SS.ReadString(1);
  cxCheckListBox1.Items[i].Checked := S2 = '+';
  Inc(i);
end;
end;

I need a help with...

procedure Tfrm.LoadStatesFromStream(SS: TStringStream);
var
  i : integer;
  S2 : String;
begin
  SS.Position := 0;
  i := 0;
while (i <= cxCheckGroup1.Properties.Items.Count - 1) and (SS.Position <   SS.Size) do
begin
  S2 := SS.ReadString(1);
  (cxCheckGroup1.States[i] = cbschecked ):= S2 = '+';  //I have a problem here

  Inc(i);
 end;
end;

Thanks for the help!


Solution

  • See the code below; I assumed that you want to include the possibility that a checkbox's state might be cbsGrayed (which I've represented by a space character in the StringStream.

    function CheckBoxStateToString(CheckBoxState : TcxCheckBoxState ) : String;
    begin
      Result := '';
      case CheckBoxState of
        cbsChecked : Result := '+';
        cbsUnChecked : Result := '-';
        cbsGrayed : Result := ' ';
      end;
    end;
    
    function StringToCheckBoxState(Input : String) : TcxCheckBoxState;
    begin
      Result := cbsGrayed;
      if Input = '+' then
        Result := cbsChecked
      else
        if Input = '-' then
          Result := cbsUnChecked
    end;
    
    procedure TForm1.SaveCheckGroupStatesToStream(SS : TStringStream);
    var
      i : integer;
    begin
      SS.Clear;
      SS.Position := 0;
      for i := 0 to cxCheckGroup1.Properties.Items.Count - 1 do begin
          SS.WriteString(CheckBoxStateToString(cxCheckGroup1.States[i]));
      end;
      Memo1.Lines.Add('>' + SS.DataString + '<');
    end;
    
    procedure TForm1.LoadCheckGroupStatesFromStream(SS : TStringStream);
    var
      i : integer;
      S : String;
    begin
      CheckBoxList.ClearCheckmarks;
      SS.Position := 0;
      i := 0;
      while (i <= cxCheckGroup1.Properties.Items.Count - 1) and (SS.Position < SS.Size) do begin
        S := SS.ReadString(1);
        cxCheckGroup1.States[i] := StringToCheckBoxState(S);
        Inc(i);
      end;
    end;