Click to See Complete Forum and Search --> : Help with CTreeCtrl.SetItemData()


cniquille
May 26th, 1999, 04:19 AM
I have some prob with CTreeCtrl.SetItemData()

I do this in OnInitialUpdate of my CTreeView class

int IDPersonne;
hItemData = ctc.InsertItem("Info",NULL,NULL, hSubTreeDirection);
bool bok = ctc.SetItemData(hItemData,IDPersonne);
DWORD dw = ctc.GetItemData(hItemData);

my variable dw has the correct number.

But in my OnDblclk() function
I Do this

CTreeCtrl &ctc = GetTreeCtrl();
HTREEITEM hTreeItem = ctc.GetParentItem(ctc.GetSelectedItem());
if (hTreeItem != NULL)
{
AfxMessageBox(ctc.GetItemText(hTreeItem));
DWORD dw = ctc.GetItemData(hTreeItem);
}

and there the dw value is 0 ??

What I wrong

Christian Niquille
Thanks for your help

YunFeiLi
May 26th, 1999, 05:07 AM
When you use SetItemData(...),you pass a auto variable to this function,It's wrong.
you can do like this:

static INT IDPersonne;
...
ctc.SetItemData(hItemData,IDPersonne);
//Or you can do it like this.
int *IDPersonne=new INT;
...
ctc.SetItemData(hItemData,*IDPersonne);
...
//now everything is OK.

Jason Teagle
May 26th, 1999, 09:22 AM
The scope of IDPersonne is not the problem. SetItemData() for any control which supports it stores a COPY of the data passed in the control's own memory space, so it does not matter if the original variable passed goes out of scope.

Jason Teagle
May 26th, 1999, 09:28 AM
The only thing I can think of is that the branch of the tree that you are retrieving is NOT the one you stored the data in.

To (dis)prove this, when you call SetItemData() in OnInitialUpdate(), make a note of the value of hItemData. Then, in the OnDblclk() routine, check the value of hTreeItem and make sure it is the same. If it is, and the value retrieved is not the same, then I can only think that something you are doing in between is corrupting the branch's data.

Perhaps the full code for the tree view class would help us to see something?