BCB TreeView AddChildObject Data
//---------------------------------------__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结点时,搞个循环,依次将Data中的数据强制转换成当初的类型,然后释放。