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

BCB TreeView AddChildObject Data,该如何处理

2012-02-09 
BCB TreeView AddChildObject DataC/C++ code//---------------------------------------__fastcall TPinf

BCB TreeView AddChildObject Data

C/C++ code
//---------------------------------------__fastcall TPinfoForm::TPinfoForm(TComponent* Owner)    : TForm(Owner){    qry_other0 = new TADOQuery(this);    qry_other1 = new TADOQuery(this);    qry_other2 = new TADOQuery(this);    qry_other3 = new TADOQuery(this);    qry_other4 = new TADOQuery(this);    qry_other5 = new TADOQuery(this);    ds_other0 = new TDataSource(this);    ds_other1 = new TDataSource(this);    ds_other2 = new TDataSource(this);    qry_other0->Connection = con_other;    qry_other1->Connection = con_other;    qry_other2->Connection = con_other;    qry_other3->Connection = con_other;    qry_other4->Connection = con_other;    qry_other5->Connection = con_other;}//---------------------------------------void __fastcall TPinfoForm::FormCreate(TObject *Sender){    qry_other0->Close();    qry_other0->SQL->Clear();    qry_other0->SQL->Add("SELECT dept_name , dept_id FROM department_info");    qry_other0->Open();    while(!qry_other0->Eof)    {         TTreeNode *deptNode = tv_info->Items->AddChildObject(tv_info->Items->Item[0]->Item[0]                                                             , qry_other0->FieldList->Fields[0]->AsString                                                             , qry_other0->FieldList->Fields[1]->AsString.c_str()                                                             );        qry_other0->Next();    }    for(int i=0; i < tv_info->Items->Item[0]->Item[0]->Count-1; i++)    {        qry_other1->Close();        qry_other1->SQL->Clear();        AnsiString deptId, deptName;        deptName = tv_info->Items->Item[0]->Item[0]->Item[i]->Text;        qry_other1->SQL->Add("SELECT doc_name , doc_id FROM doctor_info , department_info WHERE dept_id = doc_dept_id AND dept_name = '" + deptName + "'");         qry_other1->Open();        while(!qry_other1->Eof)        {             TTreeNode *docNode = tv_info->Items->AddChildObject(tv_info->Items->Item[0]->Item[0]->Item[i]                                                                , qry_other1->FieldList->Fields[0]->AsString                                                                , qry_other1->FieldList->Fields[1]->AsString.c_str());            qry_other2->Close();            qry_other2->SQL->Clear();            AnsiString docID = AnsiString((char*)docNode->Data).TrimRight();            //AnsiString docID = IntToStr((int)docNode->Data).Trim();            qry_other2->SQL->Add("SELECT p_name , p_id FROM patient_info WHERE attending_doc_id = '" + docID + "'");            qry_other2->Open();            while(!qry_other2->Eof)            {                 TTreeNode *patNode = tv_info->Items->AddChildObject(docNode                                                                    , qry_other2->FieldList->Fields[0]->AsString                                                                    , qry_other2->FieldList->Fields[1]->AsString.c_str());                qry_other2->Next();            }            qry_other1->Next();        }    }    delete qry_other0, qry_other1, qry_other2;}//---------------------------------------void __fastcall TPinfoForm::Button1Click(TObject *Sender){    AnsiString some = AnsiString((char*)tv_info->Selected->Data).Trim();    ShowMessage(some); }//---------------------------------------

以上代码是我创建TreeView和为各节点赋值的过程,在FormCreate里面访问Data值正确,但是到Button1Click中想要得到相应的Data却出错了(得到其他值,好像是Node的Text值而非Data值),请问是不是Data值得生命周期在其循环内使用之后就结束了?如果这样,我想在后续代码中得到它的值该怎么办?谢谢了。。。



[解决办法]
关于释放这个问题,当程序关闭或者要清空TreeView结点时,搞个循环,依次将Data中的数据强制转换成当初的类型,然后释放。

热点排行