Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving TTreeView data to file???

Status
Not open for further replies.

topcat01

Programmer
Jul 10, 2003
83
0
0
GB
The code below allows me to create a node in a treeview and associate data with that node the data is displayed once the node is selected through the OnClick event.

Can anybody tell me how to save all this data to file, so that it can be used used at a later date.

HThankyou.

// BEGIN unit.h FILE ----------------------
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
//---------------------------------------------------------------------------
typedef struct MyRec
{
AnsiString FName, LName;
} TMyRec;
typedef TMyRec* PMyRec;

class TForm1 : public TForm
{
__published: // IDE-managed Components
TTreeView *TreeView1;
TButton *Button1;
TEdit *Edit1;
TEdit *Edit2;
TEdit *Edit3;
TLabel *Label1;
TLabel *Label2;
TLabel *Label3;
TLabel *Label4;
TLabel *Label5;
void __fastcall Button1Click(TObject *Sender);
void __fastcall TreeView1Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

// END unit.h FILE -----------------

// BEGIN unit1.CPP ---------------------------------------
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include &quot;Unit1.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
PMyRec MyRecPtr;
int TreeViewIndex;
TTreeNodes* pItems;

MyRecPtr = new TMyRec;
MyRecPtr->FName = Edit1->Text;
MyRecPtr->LName = Edit2->Text;

TreeViewIndex = StrToInt(Edit3->Text);
pItems = TreeView1->Items;
if (pItems->Count == 0)
pItems->AddObject(NULL, &quot;Item&quot; + IntToStr(TreeViewIndex), MyRecPtr);
else if ((TreeViewIndex < pItems->Count) && (TreeViewIndex >= 0))
pItems->AddObject(pItems->Item[TreeViewIndex], &quot;Item&quot; + IntToStr(TreeViewIndex), MyRecPtr);

}
//---------------------------------------------------------------------------
void __fastcall TForm1::TreeView1Click(TObject *Sender)
{
Label1->Caption = PMyRec(TreeView1->Selected->Data)->FName + &quot; &quot; +
PMyRec(TreeView1->Selected->Data)->LName;

}
//---------------------------------------------------------------------------

// END unit1.CPP ----------------------------------------------
 
I may not completely understand what you need, but basically you just want to save all of the records to file right? I would recommend, you don't have to do this, but it might be easier to also add all new records to a TList. Tlists are very easy to use and save a lot of headache in keeping track of records, classes, pointers, etc.

Anyway, loop through the TreeView1->Items of course to get the data pointer from each treeview. Next, write that data to file. You have two ansistrings. Unfortunately, with ansistrings in your record (structure), you won't just be able to write x bytes from the structure to file. You'll need to follow the following scheme:

int FilePtr;
FilePtr = FileOpen(&quot;filename.dat&quot;, fmOpenWrite); // overwrite or create new one.

int RecCount = TreeView1->Items->Count;
FileWrite(FilePtr, &RecCount, sizeof(RecCount));

for (int x=0; x < RecCount; x++)
{
PMyRec *Rec = (PMyRec*)TreeView1->Items->Objects[x];
int Name1Length = Rec->FName.Length();
int Name2Length = Rec->LName.Length();
FileWrite(FilePtr, &Name1Length, sizeof(Name1Length));
FileWrite(FilePtr, Rec->FName.c_str(), Name1Length);
FileWrite(FilePtr, &Name2Length, sizeof(Name2Length));
FileWrite(FilePtr, Rec->LName.c_str(), Name2Length);
}

FileClose(FilePtr);

You should use FileRead to read the data back in. Just apply the same concept above. The most important thing is to lay out the file structure ahead of time.

Good luck,
Chris
 
I agree chris TList are easier to use however the use of a TreeView makes for better presentation as sub groups are required.

I have understand most of what is happening, however I am not sure what the objects[x] is for?

Are there any examples available in which a user clicks a node in a predefined tree which in turn pulls a record from file and display through Labels etc.

Thanks for your help Chris,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top