Board index » cppbuilder » TListView: How do I save the Data associated with each item?
Brian
![]() CBuilder Developer |
Brian
![]() CBuilder Developer |
TListView: How do I save the Data associated with each item?2005-01-28 07:45:42 AM cppbuilder91 I've read the post from Gambit regarding how to save the listview information and it all works beautifully. However, I am also using the Data property for each Item and have associated data of varying size. Is there a simple way to figure out the size of the data so I can save it to disk? Or, better yet, is there a simple way to save the Data piece of each TListItem? |
Remy Lebeau (TeamB)
![]() CBuilder Developer |
2005-01-28 08:20:54 AM
Re:TListView: How do I save the Data associated with each item?
"Brian" < XXXX@XXXXX.COM >wrote in message
QuoteIs there a simple way to figure out the size of the data struct Header { DWORD size; // other common values here... }; struct A { Header hdr; // A-related values here... A() { hdr.size = sizeof(A); } }; struct B { Header hdr; // B-related values here... B() { hdr.size = sizeof(B); } }; TListItem *Item = ListView1->Items->Add(); //... Item->Data = new A; Item = ListView1->Items->Add(); //... Item->Data = new B; //... int count = ListView1->Items->Count; for(int x = 0; x < count; ++x) { Header *pHdr = (Header*) ListView1->Items->Item[x]->Data; ShowMessage("Item["+AnsiString(x)+"]->Data is "+AnsiString(pHdr->size)+" bytes in size"); } If you need to actually know what data type is stored in the Data, then you have two options: 1) include an enum in the header that specfies what type is bein used: enum StructType { stA, stB }; struct Header { DWORD size; StructType type; // other common values here... }; struct A { Header hdr; // A-related values here... A() { hdr.size = sizeof(A); hdr.type = stA; } }; struct B { Header hdr; // B-related values here... B() { hdr.size = sizeof(B); hdr.type = stB; } }; int count = ListView1->Items->Count; for(int x = 0; x < count; ++x) { Header *pHdr = (Header*) ListView1->Items->Item[x]->Data; ShowMessage("Item["+AnsiString(x)+"]->Data is "+AnsiString(pHdr->size)+" bytes in size"); if( pHdr->type == stA ) { A *a = (A*) Header; //use a as needed.. } else if( pHdr->type == stB ) { B *b = (B*) Header; // use b as needed.. } } 2) derive the types from a common base class and then use polymorphism: struct Base { virtual DWORD getSize() = 0; // other common stuff here... }; struct A : public Base { // A-related values here... DWORD getSize() { return sizeof(A); } }; struct B : public Base { // B-related values here... DWORD getSize() { return sizeof(B); } }; int count = ListView1->Items->Count; for(int x = 0; x < count; ++x) { Base *pBase = (Base*) ListView1->Items->Item[x]->Data; ShowMessage("Item["+AnsiString(x)+"]->Data is "+AnsiString(pBase->getSize())+" bytes in size"); if( A *a = dynamic_cast<A*>(pBase) ) { // use a as needed.. } else if( B *b = dynamic_cast<B*>(pBase) ) { // use b as needed.. } } QuoteOr, better yet, is there a simple way to save the Data piece of each to interpret it properly. Gambit |