关于TTreeView(0)

  • 主题发起人 主题发起人 chenp1688
  • 开始时间 开始时间
C

chenp1688

Unregistered / Unconfirmed
GUEST, unregistred user!
请教这个算法的错误和改进的方法procedure MakeTree(query:TADOQuery;TableName:string;TreeView: TTreeView;FID:integer );var ShowTxt:String; Level:Integer; MyNode:array[0..6]of TTreeNode;//保存各级节点,最长支持6级(重点)begin Screen.Cursor:=crHourGlass; TreeView.Items.BeginUpdate; Level:=0; With query do begin try if query.Active=true then query.Close; Query.SQL.Text := 'SELECT * FROM ' + TableName + ' where FItemClassID='+inttostr(FID)+' ORDER BY FItemID '; //FItemID Open; First; TreeView.Items.Clear; //以下是增加第一项 MyNode[Level]:=TreeView.Items.Add(TreeView.TopItem,FieldByName('Fname').AsString); MyNode[Level].ImageIndex:=0; MyNode[Level].SelectedIndex:=1; //以上是增加第一项 While Not Eof do begin Level:=FieldByName('Flevel').AsInteger; ShowTxt:=FieldByName('Fname').AsString; // 以下用上一级节点为父节点添加子节点 if Level>0 then//确保代码符合标准 begin MyNode[Level]:=TreeView.Items.AddChild(MyNode[Level-1],ShowTxt); MyNode[Level].ImageIndex:=0; MyNode[Level].SelectedIndex:=1; end; //以 上是增加子项 Next; end; TreeView.Items.EndUpdate; query.EnableControls; finally Close; end; end; MyNode[0].Expand(false);//将首节点展开 Screen.Cursor:=crDefault;end;
 
这个 while 根本没有退出代码啊.而且按你的要求来看需要写成递归函数才可以.递归在这里面估计也有速度的问题。
 
请帮忙改进改进,其他方式也可以
 
我找了一个以前写的代码修改了一下,行不行要你自己再改了.//----------------------------------------------------type PTreeInfo=^TTreeInfo; TTreeInfo=record ID,ParentID:Integer; Name:String[128]; end;procedure GetSubList(RootID:Integer;RecordList:TList;var TmpList:TList);var i:Integer; P:PTreeInfo;begin if RecordList.Count>0 then for i:=RecordList.Count downto 1 do begin P:=RecordList.items[i-1]; if P^.ParentID=RootID then begin TmpList.Add(P); RecordList.Delete(i-1); end; end;end;procedure MakeTree(RootID:Integer;TV:TTreeView;RecordList:TList;const CurItem:TTreeNode=nil);var TmpList:TList; TmpNode:TTreeNode; i:Integer; P:PTreeInfo;begin try TmpList:=TList.Create; GetSubList(RootID,RecordList,TmpList); if TmpList.Count>0 then for i:=TmpList.Count downto 1 do begin P:=TmpList.Items[i-1]; TmpNode:=TV.Items.AddChild(CurItem,P^.Name); TmpNode.StateIndex:=P^.ID; if TmpNode.level<6 then MakeTree(P^.ID,TV,RecordList,TmpNode); end; finally FreeAndNil(TmpList); end;end;procedure InitialTreeView(TV:TTreeView;Query:TAdoQuery;TableName:String);var i:Integer; P:PTreeInfo; RecordList:TList;begin TRY RecordList:=TList.Create; query.close; Query.Sql.Clear; Query.Sql.add('SELECT * FROM ' + TableName); Query.open; for i:=1 to query.recordcount do begin New(P); P^.ID:=Query.FieldByName('FItemID').AsInteger; P^.ParentID:=Query.FieldByName('FItemClassID').AsInteger; P^.Name:=Query.FieldByName('Fname').AsString; RecordList.Add(P); end; query.close; Query.Sql.Clear; Query.Sql.add('SELECT * FROM ' + TableName+' where level=0'); Query.open; for i:=1 to Query.recordcount do MakeTree(Query.FieldByName('FItemID').AsInteger,TV,RecordList,nil); for i:=RecordList.Count downto 1 do begin Dispose(RecordList.Items[i-1]); RecordList.Items[i-1]:=nil; RecordList.Delete(i-1); end; finally FreeAndNil(RecordList); end;end;
 
欢迎加入:89862412c#讨论群!刚开的!我看其它好多群都满了,所以自己建了一个,希望和大家一起学习进步!
 
dhszxh我按你的启示和在网上下了些代码作了下列修改,虽然可以显示了,但排序还是存在题,如第一次我新增了'11'第二次增'22'第三次增'33'排列出来的结果是:计量单位  33  22  11能帮我看看问题出在哪里吗?怎样解进?procedure MakeTree(query:TADOQuery;TableName:string;TreeView: TTreeView;FID:integer);var ShowTxt:String; Index:Integer; MyNode: TTreeNode;begin // Screen.Cursor:=crHourGlass; With query do begin if query.Active=true then query.Close; Query.SQL.Text := 'SELECT * FROM ' + TableName + ' where FItemClassID='+inttostr(FID)+' ORDER BY FParentID,FItemID'; //FItemID是节点D,FParentID 是父节点 Open; First; end; TreeView.Items.BeginUpdate; try TreeView.Items.Clear; TreeView.Items.Clear; List:=TStringList.Create; try While Not query.Eof do begin if Query.FieldByName('FParentID').AsInteger = 0 then begin MyNode:=TreeView.Items.Add(TreeView.TopItem,Query.FieldByName('Fname').AsString); MyNode.ImageIndex:=0; MyNode.SelectedIndex:=1; end else begin Index:=List.IndexOf(Query.FieldByName('FParentID').AsString); MyNode :=TreeView.Items.AddChildFirst(TTreeNode(List.Objects[index]),Query.FieldByName('Fname').AsString); //MyNode :=TreeView.Items.AddChild(h*****,Query.FieldByName('Fname').AsString);//TTreeNode(List.Objects[Index]) MyNode.ImageIndex:=0; MyNode.SelectedIndex:=1; end; List.AddObject(Query.FieldByName('FItemID').AsString,MYNode); Query.Next; end; finally List.Free; Query.Close; end; finally TreeView.Items.EndUpdate; end; MyNode.Expand(false);//将首节点展开 //TreeView.AutoExpand:=true;end;
 

Similar threads

I
回复
0
查看
503
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
后退
顶部