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 "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
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, "Item" + IntToStr(TreeViewIndex), MyRecPtr);
else if ((TreeViewIndex < pItems->Count) && (TreeViewIndex >= 0))
pItems->AddObject(pItems->Item[TreeViewIndex], "Item" + IntToStr(TreeViewIndex), MyRecPtr);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TreeView1Click(TObject *Sender)
{
Label1->Caption = PMyRec(TreeView1->Selected->Data)->FName + " " +
PMyRec(TreeView1->Selected->Data)->LName;
}
//---------------------------------------------------------------------------
// END unit1.CPP ----------------------------------------------
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 "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
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, "Item" + IntToStr(TreeViewIndex), MyRecPtr);
else if ((TreeViewIndex < pItems->Count) && (TreeViewIndex >= 0))
pItems->AddObject(pItems->Item[TreeViewIndex], "Item" + IntToStr(TreeViewIndex), MyRecPtr);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TreeView1Click(TObject *Sender)
{
Label1->Caption = PMyRec(TreeView1->Selected->Data)->FName + " " +
PMyRec(TreeView1->Selected->Data)->LName;
}
//---------------------------------------------------------------------------
// END unit1.CPP ----------------------------------------------