TreeView指定结点的分支的遍历原代码,请高手尽快,急!(100分)

  • 主题发起人 主题发起人 xgd
  • 开始时间 开始时间
給你一個刪除操作,其他操作類似:
procedure DeleteAllChildData(treenode:TTreenode);
var
i :integer;
pnodeData :pnodeStruc;
begin
for i:=0 to treenode.Count-1 do
begin
if treenode.item.getFirstChild=nil then
begin
if treenode.item.data<>nil then
Dispose(treenode.item.Data);
end
else
DeleteAllChildData(treenode.Item);
end;
end;
 
我有更简洁的方法。
procedure TForm1.ScanTree1(InNode: TTreeNode);
var
CurNode:TTreeNode;
begin
CurNode:=InNode.getFirstChild;
Memo1.Lines.Add(CurNode.Text);
while (CurNode<>nil) and (CurNode.Level<>InNode.Level) do begin
Memo1.Lines.Add(CurNode.Text);
CurNode:=CurNode.GetNext;
end;
end;
 
再添一个方法。
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, ComCtrls;

type
TForm1 = class(TForm)
TreeView1: TTreeView;
Button1: TButton;
Memo1: TMemo;
Memo2: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
Alevel:integer;
procedure ScanTree1(InNode:TTreeNode);
procedure ScanTree2(InNode:TTreeNode);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
Alevel:=TreeView1.Items[4].Level;
ScanTree2(TreeView1.Items[4]);
end;


procedure TForm1.ScanTree1(InNode: TTreeNode);
var
CurNode:TTreeNode;
begin
CurNode:=InNode.getFirstChild;
while (CurNode<>nil) and (CurNode.Level<>InNode.Level) do begin
Memo1.Lines.Add(CurNode.Text);
CurNode:=CurNode.GetNext;
end;
end;

procedure TForm1.ScanTree2(InNode: TTreeNode);
var
CurNode,temp:TTreeNode;
i:integer;
begin
CurNode:=InNode;
if CurNode<>nil then
if CurNode.Parent<>nil then begin
repeat
Memo2.Lines.Add(CurNode.Text);
if CurNode.HasChildren then
ScanTree2(CurNode.getFirstChild);
CurNode:=CurNode.Parent.GetNextChild(CurNode);
until (CurNode=nil) or (ALevel=CurNode.Level);
end
else ScanTree2(CurNode.getFirstChild);
end;

end.
 
autofish :
Thank you !!!
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部