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!

TreeView->AddObject

Status
Not open for further replies.

cmsamcfe

Programmer
Dec 18, 2002
31
0
0
GB
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], &quot;Item&quot; + IntToStr(TreeViewIndex), MyRecPtr);
}



void __fastcall TForm1::Button2Click(TObject *Sender)
{
Label1->Caption = PMyRec(TreeView1->Selected->Data)->FName + &quot; &quot; +
PMyRec(TreeView1->Selected->Data)->LName;
}
 
Solved problem - needed to add a &quot;new&quot; call inside the loop to create a new instance of my structure on each iteration of the loop
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top