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!

TTreeView

Status
Not open for further replies.

cmsamcfe

Programmer
Dec 18, 2002
31
0
0
GB
I am having big problems with a TreeView component in C++Builder.

Basically I have a database table and I am trying to populate a tree based on the contents of the table. I take a directory string from the table, split it up, and then make nodes out of each of the strings, so

C:\ajm\test\001.png would give me

+ C
+ ajm
+ test

I then add the filename as a leaf. I've overcome the first hurdle of checking for duplicates so as not to create multiple nodes for files in the same directory, but my code is still not correct.... for a given list of files my output is something like this:

c:\ajm\test\001.png
c:\ajm\test\002.png
c:\ajm\images\1.bmp
c:\ajm\006.mpeg

gives

+ C
+ ajm
+ test
001.png
002.png
+ images
1.bmp
006.mpeg

so if there is more than one file with the same directory path the tree is not created correctly.

Does anyone out there have any advice or ideas on why this could be? I can post some code if necessary

Thanks
 
Dear cms...,

yes, please post some code, especially the part where You check the folders and the part where you (loop, i guess) the creation of nodes.

then we could check where Your bug is.




regards,

michael
 
Here's my code so far

-------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{

//---------------------------------------------------------
// Variable Declarations
//---------------------------------------------------------

int r_count=0, // count of the number of records in the database
l_count=0, // count of elements in the list of strings to add
n_count=0, // count of number of nodes in the tree
l_max=0, // max elements in the list
n_max=0, // max elements in the tree
chk_res=1; // result of string compare when checking for duplicates
// in the tree structure..

AnsiString temp_img_dir,
temp_img_name,
branch_info, // the record data to be added to the tree
chk_inp,
chk_node;

char *ptr;

TTreeNode *ParentNode,
*LastNode,
*NextNode;

//------------------------------------------------------------------------------

TreeView1->Items->Clear();
NextNode = TreeView1->Items->Add(NULL,"Templates");
ParentNode = NextNode; // set the parent to the node last added??

for (r_count = 0; r_count < Table1->RecordCount; r_count++)
{
//---------------------------------------------------------
//Get the data from the database to add it to the tree
//---------------------------------------------------------

temp_img_dir = Table1->FieldByName(&quot;img_directory&quot;)->AsString;
temp_img_name = Table1->FieldByName(&quot;img_filename&quot;)->AsString;

branch_info = temp_img_dir + temp_img_name;
ListBox1->Items->Add(branch_info); // Add each string to temp listbox

//----------------------------------------------------------
// Split each directory at the slashes to get the node data
//----------------------------------------------------------

ptr = strtok(branch_info.c_str(),&quot;\\&quot;);
while(ptr)
{
ListBox2->Items->Add(ptr); // data now in one big list
ptr = strtok(NULL,&quot;\\&quot;);
}

//----------------------------------------------------------
// Add each item in the list to the tree as a node
/*
Each string is dealt with seperately. The last element in the list
each time is the filename, regardless of the number of sub-folders
so when l_count == number of list elements you are dealing with the
filename, not a directory name

*/
//----------------------------------------------------------

l_max = ListBox2->Items->Count-1; // determine the max value for this string
n_max = TreeView1->Items->Count;

for (l_count = 1; l_count < l_max; l_count++) // for directories in the list
// changing l_count controls where in the list the tree starts i.e. 1->atri not C:
{
//----------------------------------------------------------
// Check the tree for existing nodes with the same name
//----------------------------------------------------------
for (n_count = 0; n_count < n_max; n_count++)
{
NextNode = TreeView1->Items->Item[n_count];
chk_inp = ListBox2->Items->Strings[l_count];
ListBox4->Items->Add(chk_inp);
chk_node = NextNode->Text;
ListBox5->Items->Add(chk_node);

chk_res = AnsiCompareStr(chk_inp,chk_node);
ListBox3->Items->Add(chk_res);

/*if (chk_res == 0)
{
ParentNode = TreeView1->Items->Item[0];
NextNode = TreeView1->Items->AddChild(ParentNode,ListBox2->Items->Strings[l_count]);
ParentNode = NextNode;
}// */

} // end of node checking for loop

NextNode = TreeView1->Items->AddChild(ParentNode,ListBox2->Items->Strings[l_count]);
ParentNode = NextNode;
} // end of list for loop

if(l_count == l_max)
{
NextNode=TreeView1->Items->AddChildFirst(ParentNode, ListBox2->Items->Strings[l_count]);
ParentNode = TreeView1->Items->Item[0];
}//*/


Label3->Caption = ListBox2->Items->Count;
ListBox2->Clear();
Table1->Next(); // Move to next record
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top