I am working with the C++ Builder example on how to add objects to a TreeView.
I have written code to add nodes to a tree, now I would like to expand this to add a structure as each node (so that I can store integer values as part of each node)
I worked through the example code, created a structure containing 5 integer values, declared a pointer to the structure, and then changed my treeview code to add the new object (with the values getting initialised to 0 each time)
I then added a button to my form to try and recall the data values from a particular node when it is pressed. The problem is that regardless of which node I click, I always get the data from the LAST node back.
I guess I am doing something stupid with the pointer (i.e. calling that rather than the node??)
Can anyone advise?
Thanks
----------------
Example code
typedef struct MyRec
{
AnsiString FName, LName;
} TMyRec;
typedef TMyRec* PMyRec;
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::Button2Click(TObject *Sender)
{
Label1->Caption = PMyRec(TreeView1->Selected->Data)->FName + " " +
PMyRec(TreeView1->Selected->Data)->LName;
}
I have written code to add nodes to a tree, now I would like to expand this to add a structure as each node (so that I can store integer values as part of each node)
I worked through the example code, created a structure containing 5 integer values, declared a pointer to the structure, and then changed my treeview code to add the new object (with the values getting initialised to 0 each time)
I then added a button to my form to try and recall the data values from a particular node when it is pressed. The problem is that regardless of which node I click, I always get the data from the LAST node back.
I guess I am doing something stupid with the pointer (i.e. calling that rather than the node??)
Can anyone advise?
Thanks
----------------
Example code
typedef struct MyRec
{
AnsiString FName, LName;
} TMyRec;
typedef TMyRec* PMyRec;
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::Button2Click(TObject *Sender)
{
Label1->Caption = PMyRec(TreeView1->Selected->Data)->FName + " " +
PMyRec(TreeView1->Selected->Data)->LName;
}