首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

用递归方式向treeview中生成树 望指教解决方法

2012-02-12 
用递归方式向treeview中生成树望指教定义了AnsiString array[13]在该数组中了存取了从数据库查询的数据,

用递归方式向treeview中生成树 望指教
定义了AnsiString array[13];在该数组中了存取了从数据库查询的数据,每个数组元素储存查询到的3个数据

C/C++ code
void AddTree( AnsiString ParentID,TTreeNode *pNode ){    AnsiString str = "PDMObject_ID" + ParentID;    TTreeNode *Node = new TTreeNode();//(1) E2285 Could not find a match for 'TTreeNode::TTreeNode()'    for( int i = 0 ; i<13 ; i++ )    if(pNode == NULL)     //处理主节点    {        Node->Text = array[i];        TreeView1->Items->Add(Node);//(2) E2451 Undefined symbol 'TreeView1'        AddTree(str,Node);    }    else                  //处理子节点    {        Node->Text = array[i];        pNode->Item->Add(Node);//(3) E2316 'Add' is not a member of 'TTreeNode'        AddTree(str,Node);    }}

在FormCreate事件中调用AddTree(0,(TTreeNode)NULL); 报错:
//(4) E2459 VCL style classes must be constructed using operator new
(2)中把TTreeView *TreeView1;的声明从__published:移到public:还是报错

[解决办法]
很乱,还不如你说清楚你要实现什么,俺重新给你写一个例子
[解决办法]
C/C++ code
  TTreeNode *root,*node,*node1;  TreeView1->Items->Clear() ;  sprintf(ls_select2,"select * from IMSDB2006..Holiday where sjdm=''") ;  DM->Query->Close() ;  DM->Query->SQL->Clear() ;  DM->Query->SQL->Add(ls_select2) ;  DM->Query->Open() ;  root=new TTreeNode(TreeView1->Items) ;  root=TreeView1->Items->AddChild(NULL,"全部") ;  fd=DM->Query->FindFirst() ;  while(fd)  {   char ls_select1[1000] ;   node1=new TTreeNode(TreeView1->Items) ;   AnsiString  ls_viewmc =DM->Query->FieldByName("HolidayCode")->AsString  ;   node1=TreeView1->Items->AddChild(root,ls_viewmc) ;   sprintf(ls_select1,"select * from Holiday where sjdm='%s'",ls_viewmc.c_str()) ;   DM->Query1->Close() ;   DM->Query1->SQL->Clear() ;   DM->Query1->SQL->Add(ls_select1) ;   DM->Query1->Open() ;   bool fd1=DM->Query1->FindFirst() ;   while(fd1)   {     node=new TTreeNode(TreeView1->Items) ;     AnsiString ls_childmc = DM->Query->FieldByName("HolidayCode")->AsString ;     node=TreeView1->Items->AddChild(node1,ls_childmc) ;     fd1=DM->Query1->FindNext() ;   }   fd=DM->Query->FindNext() ;  }
[解决办法]
我没有写代码测试,不过楼主的代码里面for循环那部分,里面递归肯定是死循环呀,没有结束条件,一直递归
改成
C/C++ code
void AddTree( AnsiString ParentID,TTreeNode *pNode ){    AnsiString str = "PDMObject_ID" + ParentID;    TTreeNode *Node ;//= new TTreeNode(NULL);//(1) E2285 Could not find a match for 'TTreeNode::TTreeNode()'    static int i = 0;    if(pNode == NULL&&i<13)     //处理主节点    {        Node = tv1->Items->AddChild(NULL,array[i]);//(2) E2451 Undefined symbol 'TreeView1'        i++;        AddTree(str,Node);    }    else                  //处理子节点    {        Node = tv1->Items->AddChild(pNode,array[i]);//(3) E2316 'Add' is not a member of 'TTreeNode'        i++;        AddTree(str,Node);    }}
[解决办法]
不太清楚,我这有个例子,看看是不是你想要的,其中ShowId是当前想要显示的节点,ParentId是当前显示节点的父节点,ShowName 显示节点的名称
C/C++ code
    tv->Items->Clear();    if( pAQry->Active )        pAQry->Active = false;    pAQry->SQL->Text = "select ShowId,ParentId,ShowName from ShowTree order by ShowId asc";    try    {        pAQry->Active = true;    }    catch(...)    {        MessageBox( this->Handle,"无法与数据库连接!","系统提示",MB_OK | MB_ICONERROR );        return false;    }    TTreeNode *ParentNode;    try    {        for( pAQry->First(); !pAQry->Eof; pAQry->Next() )        {            if( pAQry->FieldByName("ParentId")->AsInteger == 0 )                tv->Items->Add(NULL,pAQry->FieldByName("ShowName")->AsString.Trim());            else            {                ParentNode = tv->Items->Item[pAQry->FieldByName("ParentId")->AsInteger-1];                tv->Items->AddChild(ParentNode,pAQry->FieldByName("ShowName")->AsString.Trim());            }        }    }    catch(...)    {        MessageBox( this->Handle,"节点生成失败!","系统提示",MB_OK | MB_ICONERROR );        return false;    }    pAQry->Active = false; 

热点排行