treeview问题(50分)

  • 主题发起人 主题发起人 jiangxidna
  • 开始时间 开始时间
J

jiangxidna

Unregistered / Unconfirmed
GUEST, unregistred user!
我现在用treeview列表显示塔杆和线路。形式如下:<br>线路<br>&nbsp; &nbsp; 线路编号1<br>&nbsp; &nbsp; &nbsp; &nbsp; 塔杆编号1<br>&nbsp; &nbsp; &nbsp; &nbsp; 塔杆编号2<br>&nbsp; &nbsp; &nbsp; &nbsp; 塔杆编号3<br>&nbsp; &nbsp; 线路编号2<br>&nbsp; &nbsp; &nbsp; &nbsp; 塔杆编号1<br>&nbsp; &nbsp; &nbsp; &nbsp; 塔杆编号2<br>&nbsp; &nbsp; &nbsp; &nbsp; 塔杆编号3<br><br>塔杆从属于线路,即每一条线路有若干个塔杆,然后显示在其下。现在有两个adoqurey,线路编号字段为 line_num ,所在的表为 line;塔杆编号字段为 tower_num ,所在的表为 tower ,tower 表里还有个 line_num字段,和表 line 里的 line_num 字段值是一样的。我要向上面那么罗列出来,这段代码怎么写?如果我现在点在任何一个塔杆编号上,怎么样可以获得相应的线路编号和塔杆编号?有没有知道的哥们,帮小弟个忙了!
 
第一个问题,查询然后生出<br><br>第二个问题只要在onclick事件里判断点击的是哪个应该就行了吧
 
晕倒,我这有段代码,但是老是把线路编号1给读到塔杆里了,请大家帮忙看下<br>with Unit1.Form1.ADOQuery1 do<br>&nbsp; begin<br>&nbsp; node:=TreeView1.Items.AddFirst(nil,'线路编号');<br>&nbsp; &nbsp; if not Active then<br>&nbsp; &nbsp; &nbsp; Open;<br>&nbsp; &nbsp; &nbsp; ADOQuery1.Close;<br>&nbsp; &nbsp; &nbsp; ADOQuery1.SQL.Add('select distinct( line_num ) from line &nbsp;order by line_num');<br>&nbsp; &nbsp; &nbsp; ADOQuery1.Open;<br>&nbsp; &nbsp; for i := 0 to RecordCount - 1 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; node1:= TreeView1.Items.AddChild(node,Trim(FieldByName('line_num').AsString));<br>&nbsp; &nbsp; &nbsp; TreeView1.Selected:=node1;<br>&nbsp; &nbsp; &nbsp; &nbsp; temp &nbsp; := &nbsp;fieldbyname('line_num').AsString;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with ADOQuery2 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if not Active then Open;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADOQuery2.Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADOQuery2.SQL.Add('Select tower_num From tower Where line_num = &nbsp;'''+temp+'''order by tower_num');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADOQuery2.Open;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for j := 0 to RecordCount - 1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;node2:= TreeView1.Items.AddChild(node1,Trim(FieldByName('tower_num').AsString));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TreeView1.Selected:=node2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br><br>&nbsp; &nbsp; &nbsp; next;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; end;
 
function TForm1.AddChildNode(vNode: TTreeNode; vName, vCode: string): TTreeNode;<br>begin<br>&nbsp; Result := TreeView1.Items.AddChild(vNode, vName + '[' + vCode + ']');<br>&nbsp; TreeView1.Selected := Result;<br>end;<br><br>function TForm1.AddNode(vNode: TTreeNode; vName: string): TTreeNode;<br>begin<br>&nbsp; Result := TreeView1.Items.AddChild(vNode, vName);<br>&nbsp; TreeView1.Selected := Result;<br>end;<br><br>procedure TForm1.FormCreate(Sender: TObject);<br>var<br>&nbsp; vTreeNode1, vTreeNode2,vTreeNode3 : TTreeNode;<br>begin<br>&nbsp; TreeView1.Items.Clear;<br>&nbsp; vTreeNode1 := AddNode(nil,'线路');<br>&nbsp; with qryLine do<br>&nbsp; begin<br>&nbsp; &nbsp; close;<br>&nbsp; &nbsp; Open;<br>&nbsp; &nbsp; First;<br>&nbsp; &nbsp; while not qryLine.Eof do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; vTreeNode2 := AddChildNode(vTreeNode1,fieldbyname('name').AsString,fieldbyname('line_num').AsString);<br>&nbsp; &nbsp; &nbsp; with qryTower do<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; close;<br>&nbsp; &nbsp; &nbsp; &nbsp; ParamByName('line_num').AsInteger := qryLine.fieldbyname('line_num').AsInteger;<br>&nbsp; &nbsp; &nbsp; &nbsp; Open;<br>&nbsp; &nbsp; &nbsp; &nbsp; First;<br>&nbsp; &nbsp; &nbsp; &nbsp; while not qryTower.Eof do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vTreeNode3 := AddChildNode(vTreeNode2,fieldbyname('name').AsString,fieldbyname('id').AsString);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; next;<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; next;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br>获得相应的线路编号和塔杆编号方法:<br>取出所选节点的Caption和父节点的Caption查找"[","]"内的数字即可。要判断父节点是否是根节点。
 
这个问题我也遇到过,当时没有解决先放下了,现在又想起来了,希望能从大家那里得到灵感,请大家多发言,给一点提示。
 
to 骑驴逛街<br>能不能将里面的变量中文解释下!谢谢了
 
函数1是增加子节点函数,在指定节点vNode上增加子节点,后面变量是节点名。<br>函数2是增加根节点函数。<br>两函数调用后分别返回一个节点。 //重点<br>vTreeNode1, vTreeNode2,vTreeNode3 : TTreeNode; &nbsp;//根节点,二级节点,三级节点
 
with UAppDataModule.dmAppDataModule.SQLQuery1 do<br>&nbsp; begin<br>&nbsp; node:=TreeView1.Items.AddFirst(nil,'线路编号');<br>&nbsp; &nbsp; &nbsp; Close;<br>&nbsp; &nbsp; &nbsp; SQL.Add('select distinct line_num &nbsp;from line &nbsp;order by line_num');<br>&nbsp; &nbsp; &nbsp; Open;<br>&nbsp; &nbsp; while not eof do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; temp &nbsp; := &nbsp;fieldbyname('line_num').AsString;<br>&nbsp; &nbsp; &nbsp; node1:= TreeView1.Items.AddChild(node,Trim(FieldByName('line_num').AsString));<br>&nbsp; &nbsp; &nbsp; TreeView1.Selected:=node1;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; with UAppDataModule.dmAppDataModule.SQLQuery2 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ssql := &nbsp;'Select tower_num &nbsp;From tower Where line_num = &nbsp;'''+temp+''' order by tower_num';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SQL.Add(Ssql);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Open;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while not eof do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;node2:= TreeView1.Items.AddChild(node1,Trim(FieldByName('tower_num').AsString));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TreeView1.Selected:=node2;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;next;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br><br>&nbsp; &nbsp; &nbsp; next;<br>&nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; end;<br><br>您看下这个怎么改?显示的结果是:线路都显示了,但是每条线路只是显示了线路1的数据,并不是把相应的数据显示出来了
 
应该没问题了吧,不清楚你的数据结构是什么样?<br>检查你的查询语句是否正确?
 
查询语句没有错的<br>也都查出了我要的结果<br>现在我有两条线,第一次循环后,显示正常,第二次循环时,线路取值正确,但是此时塔杆取值还是第一次塔杆的取值,没有变化,我总觉得是不是没有释放第一次查询结果,有没有高手看出来哪不对劲?
 
sql语句没有加sql.clear
 
接受答案了.
 
后退
顶部