save all values in listbox with checked boolean flag


i have tried to save all values in listview and can't save the checkbox with the file, how is this possible? more points for quick and good answer
palli011000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RadlerCommented:
I'm sending example located at Delphi doc's

function ComponentToString(Component: TComponent): string;

var
  BinStream:TMemoryStream;
  StrStream: TStringStream;
  s: string;
begin
  BinStream := TMemoryString.Create;
  try
    StrStream := TStringStream.Create(s);
    try
      BinStream.WriteComponent(Component);
      BinStream.Seek(0, soFromBeginning);
      ObjectBinaryToText(BinStream, StrStream);
      StrStream.Seek(0, soFromBeginning);
      Result:= StrStream.DataString;
    finally
      StrStram.Free;
    end;

  finally
    BinStream.Free
  end;
end;

function StringToComponent(Value: string): TComponent;
var
  StrStream:TStringStream;
  BinStream: TMemoryStream;
begin
  StrStream := TStringStream.Create(Value);
  try
    BinStream := TMemoryStream.Create;
    try
      ObjectTextToBinary(StrStream, BinStream);
      BinStream.Seek(0, soFromBeginning);
      Result := BinStream.ReadComponent(nil);

    finally
      BinStream.Free;
    end;
  finally
    StrStream.Free;
  end;
end;

T++, Radler.
0
rwilson032697Commented:
Listening
0
palli011000Author Commented:
how do i use this function , example please with listview
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

RadlerCommented:
Sorry,

The property Checked isn't published. To complete this solution you need save the state isolatly.
For i:=0 to Lst.ItemsCount-1 do begin
//Put the state here
end;
0
palli011000Author Commented:
how do i use this function , example please with listview
0
palli011000Author Commented:
I get error on this
BinStream := TMemoryString.Create

How can i save it to file e.g list.dat
and load it back into listview on form creae?
0
palli011000Author Commented:
i will be very glad if you can write complete example for me on button1click and button2click to load it back

Thanks, i raise the points for the example
0
palli011000Author Commented:
i will be very glad if you can write complete example for me on button1click and button2click to load it back

Thanks, i raise the points for the example
0
RadlerCommented:
Hi Palli;

I have a complete code near 100% tested. Very clear by signal
I'm hungry for points( >= 200 + A is ok ).
I'm waiting a hour to go to home.

T++, Radler.
0
palli011000Author Commented:
alright you will get 200 points!!!

thanks
0
RadlerCommented:
Remember 200 points plus a A grade. I need justify my time at E-E and I'm far from top 15 list.

Well, here is the DFM
object Form1: TForm1
  Left = 341
  Top = 103
  Width = 870
  Height = 640
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ListView1: TListView
    Left = 312
    Top = 104
    Width = 185
    Height = 129
    Checkboxes = True
    Columns = <>
    HotTrackStyles = []
    Items.Data = {
      600000000400000001000000FFFFFFFFFFFFFFFF000000000000000001310200
      0000FFFFFFFFFFFFFFFF0000000000000000013203000000FFFFFFFFFFFFFFFF
      0000000000000000013300000000FFFFFFFFFFFFFFFF00000000000000000134}
    LargeImages = ImageList1
    TabOrder = 0
  end
  object SaveBtn: TButton
    Left = 272
    Top = 392
    Width = 75
    Height = 25
    Caption = 'Save'
    TabOrder = 1
    OnClick = SaveBtnClick
  end
  object LoadBtn: TButton
    Left = 496
    Top = 392
    Width = 75
    Height = 25
    Caption = 'LoadBtn'
    TabOrder = 2
    OnClick = LoadBtnClick
  end
  object ClearItems: TButton
    Left = 368
    Top = 320
    Width = 75
    Height = 25
    Caption = 'ClearItems'
    TabOrder = 3
    OnClick = ClearItemsClick
  end
  object ImageList1: TImageList
    Left = 136
    Top = 128
    Bitmap = {
      3608000004000000424D36080000000000003600000028000000400000001000
      000001001000000000000008000000000000000000000000000000000000EF3D
      00000000000000000F000F0000000000000000000000000000000000EF3D0000
      00000000000000000F000F000000000000000000000000000000000000000000
      00000000000000000F000F000000000000000000000000000000000000000000
      00000000003CE03D00000000000000000000E03D003C00000000000000000000
      000000000000000000000000EF3D000000000000F75E000000000000EF3D0000
      0000000000000000000000000F00000000000000F75E0000000000000F000000
      0000000000000000000000000F00000000000000F75E0000000000000F000000
      0000003C000000000000007C0000007C000000000000003C0000000000000000
      0000000000000000000000001F0000000000000000000000000000000F000000
      0000000000000000000000001F0000000000000000000000000000000F000000
      0000000000000000000000001F0000000000000000000000000000000F000000
      000000000000007CF75EFF7F0000FF7FF75E007C000000000000000000000000
      0000000000000000EF3DF75EEF3DF75E1F00EF3D000000000000000000000000
      00000000000000000F000000EF3D00001F000F00000000000000000000000000
      00000000000000000F000000EF3D00001F000F00000000000000000000000000
      EF3D0000007CF75E0000FF7FFF7FFF7F0000F75E007C0000EF3D000000000000
      00000000EF3D1F00F75EF75EF75EF75EF75EF75EEF3D00000000000000000000
      000000000F001F0000000000F75E0000000000000F0000000000000000000000
      000000000F001F0000000000F75E0000000000000F0000000000000000000000
      0000007C007CFF7FFF7F0000FF7FFF7FFF7FFF7F007C007C0000000000000000
      000000001F00F75EF75EEF3DF75EEF3DF75EF75EF75EEF3D0000000000000000
      000000001F00000000000F00F75E0F000000000000000F000000000000000000
      000000001F00000000000F00F75E0F000000000000000F000000000000000000
      0000007C0000FF7FFF7FFF7F0000FF7FFF7F00000000007C0000000000000000
      0000EF3DF75EF75EEF3DEF3DF75EEF3DEF3DF75EF75EF75E0000000000000000
      00000F000000000000000000F75E000000000000000000000000000000000000
      00000F000000000000000000F75E000000000000000000000000000000000000
      0000007C007CFF7FFF7FFF7F0000FF7FFF7FFF7F007C007C0000000000000000
      0000F75EF75EEF3DEF3DF75EE03DF75EEF3DEF3DF75EF75EEF3D000000000000
      0000000000000F000000F75EFF7FF75E00000F00000000000F00000000000000
      0000000000000F000000F75EFF7FF75E00000F00000000000F00000000000000
      EF3D0000007CF75E0000FF7F0000FF7F0000F75E007C0000EF3D00000000EF3D
      1F00EF3DF75EEF3DF75EE03DE03DE03DE03DE03DF75EEF3D1F00EF3D00000F00
      1F00EF3DF75EEF3DF75EFF7FFF7FFF7FF75EF75EF75EEF3D1F000F0000000F00
      1F00EF3DF75EEF3DF75EFF7FFF7FFF7FF75EF75EF75EEF3D1F000F0000000000
      000000000000007CF75EFF7F0000FF7FF75E007C000000000000000000000000
      0000F75EF75EEF3DEF3DF75EE03DF75EEF3DEF3DF75EF75EEF3D000000000000
      0000000000000F000000F75EFF7FF75E00000F00000000000F00000000000000
      0000000000000F000000F75EFF7FF75E00000F00000000000F00000000000000
      0000000000000000007C007C007C007C007C0000000000000000000000000000
      0000EF3DF75EF75EEF3DEF3DF75EEF3DEF3DF75EF75EF75E0000000000000000
      00000F000000000000000000F75E000000000000000000000000000000000000
      00000F000000000000000000F75E000000000000000000000000000000000000
      F75EF75EEF3D0000000000000000000000000000F75EEF3DEF3D000000000000
      000000001F00F75EF75EEF3DF75EEF3DF75EF75EF75EEF3D0000000000000000
      000000001F00000000000F00F75E0F000000000000000F000000000000000000
      000000001F00000000000F00F75E0F000000000000000F000000000000000000
      F75EF75EF75EF75EEF3DEF3D0000EF3DF75EF75EF75EF75EEF3D000000000000
      00000000EF3D1F00F75EF75EF75EF75EF75EF75EEF3D0000E03D0000EF3D0000
      000000000F001F0000000000F75E0000000000000F000000EF3D00000F000000
      000000000F001F0000000000F75E0000000000000F000000EF3D00000F000000
      0000EF3DF75EF75E0000EF3D0000EF3D0000F75EF75EEF3D0000000000000000
      0000000000000000EF3DF75EEF3DF75E1F00EF3D000000000000000000000000
      00000000000000000F000000EF3D00001F000F00000000000000000000000000
      00000000000000000F000000EF3D00001F000F00000000000000000000000000
      0000EF3D000000000000000000000000000000000000EF3D0000000000000000
      1F00000000000000000000001F000000000000000000000000000000EF3D0000
      1F00000000000000000000001F0000000000000000000000000000000F000000
      1F00000000000000000000001F0000000000000000000000000000000F000000
      000000000000000000000000000000000000000000000000000000000000EF3D
      0000000000000F0000000000EF3D00000000000000000000000000000000EF3D
      0000000000000F00000000000F0000000000000000000000000000000000EF3D
      0000000000000F00000000000F00000000000000000000000000000000000000
      000000000000000000000000000000000000000000000000000000000000424D
      BE000000000000003E0000002800000040000000100000000100010000000000
      800000000000000000000000020000000000000000000000FFFFFF0079FE79FE
      79FE8003FEEEFEEEFEEEC007FEFEFEFEFEFEC007F81FF81FF81F8003E00FE00F
      E00F8003E007E007E0078003C007C007C0078003C003C003C003800300010001
      0001C007C003C003C0030001C007C007C0070001E007E007E0070001E00AE00A
      E00A8003F81FF81FF81FC6C7BEFEBEFEBEFEFC7F76FF76FF76FFFFFF}
  end
end

is necessary only to make a sample.

here is the code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Controls, Forms,
  StdCtrls, Spin, ComCtrls, ImgList;

type
  TForm1 = class(TForm)
       ListView1: TListView;
       SaveBtn: TButton;
       LoadBtn: TButton;
       ImageList1: TImageList;
       ClearItems: TButton;
       procedure SaveBtnClick(Sender: TObject);
       procedure LoadBtnClick(Sender: TObject);
       procedure ClearItemsClick(Sender: TObject);
  private
       { Private declarations }
  public
       { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}


procedure TForm1.SaveBtnClick(Sender: TObject);
var
      Stream : TMemoryStream;
      i, State : DWORD;
begin
      Stream:=TMemoryStream.Create;
      Stream.WriteComponent( Self.ListView1 );
      State:=0;
      for i:=0 to ListView1.Items.Count-1 do begin
            if ListView1.Items.Item[i].Checked then begin
                  Inc( State, 1 shl i );
            end;
      end;
      Stream.Write( State, SizeOf( State ));
      Stream.SaveToFile( 'c:\comp.txt');
      Stream.Free;
end;


procedure TForm1.LoadBtnClick(Sender: TObject);
var
      Stream : TMemoryStream;
      i, State : DWORD;
begin
      ListView1.Items.Clear;
      Stream:=TMemoryStream.Create;
      Stream.LoadFromFile( 'c:\comp.txt' );
      Stream.ReadComponent( ListView1 );
      Stream.Seek( -SizeOf( State ), soFromEnd );
      Stream.Read( State, SizeOf( State ));
      Stream.Free;
      for i:=0 to ListView1.Items.Count-1 do begin
            if (State and ( 1 shl i ) <> 0) then begin
                  ListView1.Items.Item[i].Checked:=True;
            end else begin
                  ListView1.Items.Item[i].Checked:=False;
            end;
      end;
end;

procedure TForm1.ClearItemsClick(Sender: TObject);
begin
      ListView1.Items.Clear;
end;

end.

If you have any dificulty tell me.
Dont accept the answer before raise the points.

T++, Radler.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadlerCommented:
I found other 2 question from you. Why ?
Your problem is solved now.

T++, Radler.
0
palli011000Author Commented:
i know!!! i don't know how these quesiotn will came there, i have delete them, but this answer is Excellent, this works very well. But one question?

Will you help me put this into function?


0
palli011000Author Commented:
i know!!! i don't know how these quesiotn will came there, i have delete them, but this answer is Excellent, this works very well. But one question?

Will you help me put this into function?


0
palli011000Author Commented:
Can you put this into function that i can call anywhere in my project

Excellent Answer
0
RadlerCommented:
Well, the main focus resides at the events associated with the buttons: Save and load the code inside them can be puted at any location with loose of generality.
You have a debt of 150 points palli, and I'm far from top 15 yet . The better codes are the smaller.
Put another question with more details.

T++, Radler.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.