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;