function TForm1.AddChildNode(vNode: TTreeNode; vName, vCode: string): TTreeNode;<br>begin<br> Result := TreeView1.Items.AddChild(vNode, vName + '[' + vCode + ']');<br> TreeView1.Selected := Result;<br>end;<br><br>function TForm1.AddNode(vNode: TTreeNode; vName: string): TTreeNode;<br>begin<br> Result := TreeView1.Items.AddChild(vNode, vName);<br> TreeView1.Selected := Result;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br> vTreeNode1, vTreeNode2,vTreeNode3 : TTreeNode;<br>begin<br> TreeView1.Items.Clear;<br> vTreeNode1 := AddNode(nil,'线路');<br> with qryLine do<br> begin<br> close;<br> Open;<br> First;<br> while not qryLine.Eof do<br> begin<br> vTreeNode2 := AddChildNode(vTreeNode1,fieldbyname('name').AsString,fieldbyname('line_num').AsString);<br> with qryTower do<br> begin<br> close;<br> ParamByName('line_num').AsInteger := qryLine.fieldbyname('line_num').AsInteger;<br> Open;<br> First;<br> while not qryTower.Eof do<br> begin<br> vTreeNode3 := AddChildNode(vTreeNode2,fieldbyname('name').AsString,fieldbyname('id').AsString);<br> next;<br> end;<br> end;<br> next;<br> end;<br> end;<br>end;<br><br>获得相应的线路编号和塔杆编号方法:<br>取出所选节点的Caption和父节点的Caption查找"[","]"内的数字即可。要判断父节点是否是根节点。