Board index » cppbuilder » Problem with TTreeView and AddChild (or my code)???

Problem with TTreeView and AddChild (or my code)???


2007-06-06 01:27:15 AM
cppbuilder45
Hello all (i hope this is the right newsgroup...),
i've been scratching my head with the function below for days and would
appreciate some help. The objective of this function is to recursively look
through a directory and subdirectory for all folders that contain a
umfolder.ini file and add them to a Treeview control. That all works fine
but the function fails when the line to call Addchild is called.
The NewNode ptr is not initialised correctly and hence the remaining
assignments to NewNode fails. I know the searching etc works fine as i have
removed the TTreeview code and tested.
At the time the AddChild is called, the tNode, tv and asFolder variables are
all valid
void __fastcall TCC3UserFolders::GetUserContainerFolders(AnsiString
StartPath,TTreeView *tv,TTreeNode *tNode)
{
TSearchRec sr;
AnsiString asPath,asUMini,asFolder;
//TTreeNode *NewNode;
asPath = StartPath+"\\*.*";
if(FindFirst(asPath, 0x0000FFFF, sr) == 0)
{
do
{
if((sr.Attr & faDirectory)>0)
{
// sr.Name is a subdirectory
if((sr.Name!=".")&&(sr.Name!=".."))
{
asUMini=StartPath+"\\"+sr.Name+"\\UMFolder.ini";
if(FileExists(asUMini)) //Stops search going too deep
{
asFolder=sr.Name;
TTreeNode *NewNode =tv->Items->AddChild(tNode,asFolder); //@@LINE
DOES NOT WORK CORRECTLY
NewNode->ImageIndex =2;
NewNode->SelectedIndex =3;
NewNode->Data = new
AnsiString(StartPath+"\\"+sr.Name);
GetUserContainerFolders(StartPath+"\\"+sr.Name,tv,NewNode);
}
}
}
}
while(FindNext(sr) == 0);
FindClose(sr);
}
}
Can any bright spark spot anything obvious that i just cannot see??
All help appreciated
Thanks
Andy
 
 

Re:Problem with TTreeView and AddChild (or my code)???

"A Johnson" < XXXX@XXXXX.COM >wrote in message
Quote
Hello all (i hope this is the right newsgroup...),
i've been scratching my head with the function below for days and would
appreciate some help. The objective of this function is to recursively
look
through a directory and subdirectory for all folders that contain a
umfolder.ini file and add them to a Treeview control. That all works fine
but the function fails when the line to call Addchild is called.

The NewNode ptr is not initialised correctly and hence the remaining
assignments to NewNode fails. I know the searching etc works fine as i
have
removed the TTreeview code and tested.

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

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

asPath = StartPath+"\\*.*";

if(FindFirst(asPath, 0x0000FFFF, sr) == 0)
{
do
{
if((sr.Attr & faDirectory)>0)
{
// sr.Name is a subdirectory
if((sr.Name!=".")&&(sr.Name!=".."))
{
asUMini=StartPath+"\\"+sr.Name+"\\UMFolder.ini";
if(FileExists(asUMini)) //Stops search going too deep
{
asFolder=sr.Name;
TTreeNode *NewNode =tv->Items->AddChild(tNode,asFolder);
//@@LINE
DOES NOT WORK CORRECTLY
NewNode->ImageIndex =2;
NewNode->SelectedIndex =3;
NewNode->Data = new
AnsiString(StartPath+"\\"+sr.Name);
GetUserContainerFolders(StartPath+"\\"+sr.Name,tv,NewNode);
}
}
}
}
while(FindNext(sr) == 0);
FindClose(sr);
}
}

Can any bright spark spot anything obvious that i just cannot see??
Your function works perfectly fine for me (BCB6). Are you *sure* that
'tNode' is valid when you call the function for the first time?
- Dennis
 

Re:Problem with TTreeView and AddChild (or my code)???

Hi, thanks for the reply. It is indeed valid - i've double checked and
double checked again - im using BDS2006 on a win2003 system. I have a
suspicion the code works fine on a win2000 system but i cannot check right
now (tomorrow). Should the OS version make a difference? Also, im calling
this function from within the Form's constructor - not sure if that makes
any diff. Immediately prior to the first call to this function, node's are
added with no problem. I just cannot figure it out...
"Dennis Jones" < XXXX@XXXXX.COM >wrote in message
Quote
>Your function works perfectly fine for me (BCB6). Are you *sure* that
'tNode' is valid when you call the function for the first time?

- Dennis

 

{smallsort}

Re:Problem with TTreeView and AddChild (or my code)???

"A Johnson" < XXXX@XXXXX.COM >wrote in message
Quote
the function fails when the line to call Addchild is called.
You did not show the code that is initially calling
GetUserContainerFolders().
Quote
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.
Quote
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.
Quote
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