Board index » cppbuilder » Saving/Loading TListView

Saving/Loading TListView


2004-09-02 04:44:39 AM
cppbuilder38
I have a TListView in Report-mode and I need to save and load it's state,
checkboxes and everything.
UnfortunatelyI don't know how, I've tried to search more or less the entire
net with google :) but I haven't found anything I can use.
Well actually I found one code, but it didn't save the checkboxes states,
WriteComponentRes/ReadComponentRes.
So any help would be useful, links or even code-snippets.
Thanks in advance, Stefan
 
 

Re:Saving/Loading TListView

"Stefan L" < XXXX@XXXXX.COM >wrote in message
Quote
I have a TListView in Report-mode and I need to
save and load it's state, checkboxes and everything.
TListView does not natively expose that much saving/restoring functionality.
You will have to handle that stuff yourself manually, for example:
bool __fastcall ReadBool(TStream *Stream)
{
bool Result = false;
Stream->Read(&Result, sizeof(bool));
return Result;
}
void __fastcall WriteBool(TStream *Stream, bool Value)
{
Stream->Write(&Value, sizeof(bool));
}
int __fastcall ReadInteger(TStream *Stream)
{
int Result = 0;
Stream->Read(&Result, sizeof(int));
return Result;
}
void __fastcall WriteInteger(TStream *Stream, int Value)
{
Stream->Write(&Value, sizeof(int));
}
AnsiString __fastcall ReadString(TStream *Stream)
{
int length = ReadInteger(Stream);
if( length>0 )
{
AnsiString Result;
Result.SetLength(length);
Stream->Read(Result.c_str(), length);
return Result;
}
return "";
}
void __fastcall WriteString(TStream *Stream, const AnsiString &Value)
{
int length = Value.Length();
WriteInteger(Stream, length);
if( length>0 )
Stream->Write(Value.c_str(), length);
}
void __fastcall ReadStrings(TStream *Stream, TStrings *Values)
{
int count = ReadInteger(Stream);
Values->BeginUpdate();
try
{
Values->Clear();
for(int x = 0; x < count; ++x)
Values->Append(ReadString(Stream));
}
__finally {
Values->EndUpdate();
}
}
void __fastcall WriteStrings(TStream *Stream, TStrings *Values)
{
int count = Values->Count;
WriteInteger(Stream, count);
for(int x = 0; x < count; ++x)
WriteString(Stream, Values->Strings[x]);
}
void __fastcall SaveListItems(TStream *Stream, TListItems *Items)
{
int count = Items->Count;
WriteInteger(Stream, count);
for(int x = 0; x < count; ++x)
{
TListItem *Item = Items->Item[x];
WriteString(Stream, Item->Caption);
WriteStrings(Stream, Item->SubItems);
WriteBool(Stream, Item->Checked);
WriteBool(Stream, Item->Cut);
WriteInteger(Stream, Item->ImageIndex);
// other values you are interested in ...
}
}
void __fastcall LoadListItems(TStream *Stream, TListItems *Items)
{
int count = ReadInteger(Stream);
Items->BeginUpdate();
try
{
Items->Clear();
for(int x = 0; x < count; ++x)
{
TListItem *Item = Items->Add();
Item->Caption = ReadString(Stream);
ReadStrings(Stream, Item->SubItems);
Item->Checked = ReadBool(Stream);
Item->Cut = ReadBool(Stream);
Item->ImageIndex = ReadInteger(Stream);
// other values you are interested in ...
}
}
__finally {
Items->EndUpdate();
}
}
Then you can call it like this:
SaveListItems(SomeTargetStream, ListView1->Items);
//...
LoadListItems(SomeSourceStream, ListView1->Items);
Gambit
 

Re:Saving/Loading TListView

Great! Thanks, you really live up to your reputation :)
/Stefan L
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >skrev i meddelandet
Quote

"Stefan L" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>I have a TListView in Report-mode and I need to
>save and load it's state, checkboxes and everything.

TListView does not natively expose that much saving/restoring
functionality.
You will have to handle that stuff yourself manually, for example:

bool __fastcall ReadBool(TStream *Stream)
{
bool Result = false;
Stream->Read(&Result, sizeof(bool));
return Result;
}

void __fastcall WriteBool(TStream *Stream, bool Value)
{
Stream->Write(&Value, sizeof(bool));
}

int __fastcall ReadInteger(TStream *Stream)
{
int Result = 0;
Stream->Read(&Result, sizeof(int));
return Result;
}

void __fastcall WriteInteger(TStream *Stream, int Value)
{
Stream->Write(&Value, sizeof(int));
}

AnsiString __fastcall ReadString(TStream *Stream)
{
int length = ReadInteger(Stream);
if( length>0 )
{
AnsiString Result;
Result.SetLength(length);
Stream->Read(Result.c_str(), length);
return Result;
}
return "";
}

void __fastcall WriteString(TStream *Stream, const AnsiString &Value)
{
int length = Value.Length();
WriteInteger(Stream, length);
if( length>0 )
Stream->Write(Value.c_str(), length);
}

void __fastcall ReadStrings(TStream *Stream, TStrings *Values)
{
int count = ReadInteger(Stream);
Values->BeginUpdate();
try
{
Values->Clear();
for(int x = 0; x < count; ++x)
Values->Append(ReadString(Stream));
}
__finally {
Values->EndUpdate();
}
}

void __fastcall WriteStrings(TStream *Stream, TStrings *Values)
{
int count = Values->Count;
WriteInteger(Stream, count);
for(int x = 0; x < count; ++x)
WriteString(Stream, Values->Strings[x]);
}


void __fastcall SaveListItems(TStream *Stream, TListItems *Items)
{
int count = Items->Count;
WriteInteger(Stream, count);
for(int x = 0; x < count; ++x)
{
TListItem *Item = Items->Item[x];
WriteString(Stream, Item->Caption);
WriteStrings(Stream, Item->SubItems);
WriteBool(Stream, Item->Checked);
WriteBool(Stream, Item->Cut);
WriteInteger(Stream, Item->ImageIndex);
// other values you are interested in ...
}
}

void __fastcall LoadListItems(TStream *Stream, TListItems *Items)
{
int count = ReadInteger(Stream);
Items->BeginUpdate();
try
{
Items->Clear();
for(int x = 0; x < count; ++x)
{
TListItem *Item = Items->Add();
Item->Caption = ReadString(Stream);
ReadStrings(Stream, Item->SubItems);
Item->Checked = ReadBool(Stream);
Item->Cut = ReadBool(Stream);
Item->ImageIndex = ReadInteger(Stream);
// other values you are interested in ...
}
}
__finally {
Items->EndUpdate();
}
}

Then you can call it like this:

SaveListItems(SomeTargetStream, ListView1->Items);
//...
LoadListItems(SomeSourceStream, ListView1->Items);


Gambit


 

{smallsort}

Re:Saving/Loading TListView

Actually something else came up, not with the saving though, that works
great.
Now I'm wondering if anyone know how to put a ComboBox or something like it
in one of the columns.
You know the ones you can change the text in an Item but only to some
pre-defined texts.
Preferably the ability to select one Item named "..." or something so a box
pops up and you can type in whatever too.
Hope I made some sense, it's late here :)
Any questions, just ask, ok?
/Stefan
"Stefan L" < XXXX@XXXXX.COM >skrev i meddelandet
Quote
I have a TListView in Report-mode and I need to save and load it's state,
checkboxes and everything.
UnfortunatelyI don't know how, I've tried to search more or less the
entire
net with google :) but I haven't found anything I can use.
Well actually I found one code, but it didn't save the checkboxes states,
WriteComponentRes/ReadComponentRes.
So any help would be useful, links or even code-snippets.

Thanks in advance, Stefan