Re: Problem with TTreeView and AddChild (or my code)??? - Fixed but how and why?!?


2007-06-06 03:36:10 AM
cppbuilder26
Thanks Remy,
your code pointed me in the right direction but i dont understand how or why
the fix works! The only bit i changed to begin with (but had the desired
effect) was this:
I broke the line
TTreeNode *NewNode =tv->Items->AddChild(tNode,asFolder);
Into two lines, the declaration and then the initialisation i.e.
TTreeNode *NewNode;
NewNode =tv->Items->AddChild(tNode,asFolder);
and that was it??? I dont understand why though and would like to in order i
dont make the same mistake again..
Regardless of that,many many thanks - i might get some sleep tonight!
Andy
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"A Johnson" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>the function fails when the line to call Addchild is called.

You did not show the code that is initially calling
GetUserContainerFolders().

>The NewNode ptr is not initialised correctly and hence
>the remaining assignments to NewNode fails.

The only way that can happen in the code you showed is if your
original node is invalid to begin with.

>I know the searching etc works fine as i have removed the
>TTreeview code and tested.

The searching code could use some minor tweaking, though.

>At the time the AddChild is called, the tNode, tv and asFolder
>variables are all valid

Obviously not, or you wouldn't be having this problem in the first
place.

Try the following:

void __fastcall TCC3UserFolders::GetUserContainerFolders(const
AnsiString &StartPath, TTreeView *tv, TTreeNode *tNode)
{
TSearchRec sr;
AnsiString asPath, asFolder;

asPath = IncludeTrailingBackslash(StartPath);

if( FindFirst(asPath + "*.*", faAnyFile, sr) == 0 )
{
do
{
if( sr.Attr & faDirectory )
{
// sr.Name is a subdirectory
if( (sr.Name != ".") && (sr.Name != "..") )
{
asFolder = asPath + sr.Name;

if( FileExists(asFolder + "\\UMFolder.ini") )
//Stops search going too deep
{
TTreeNode *NewNode;

if( tNode )
NewNode = tv->Items->AddChild(tNode,
sr.Name);
else
NewNode = tv->Items->Add(NULL,
sr.Name);

NewNode->ImageIndex = 2;
NewNode->SelectedIndex = 3;
NewNode->Data = new AnsiString(asFolder);

GetUserContainerFolders(asFolder, tv,
NewNode);
}
}
}
}
while( FindNext(sr) == 0 );
FindClose(sr);
}
}


Gambit