procedure TForm1.AddTree(Path: string; ParentNode: TTreeNode);
var
SearchRec : TSearchRec;
IFound : integer;
newnode : TTreeNode;
begin
IFound := FindFirst(Path + '\*.*', faAnyFile, SearchRec);
While IFound = 0 do
begin
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
begin
if SearchRec.Attr = faDirectory then
begin
newnode := TreeView1.Items.AddChild(ParentNode, SearchRec.Name);
AddTree(Path + '\' + SearchRec.Name, newnode);
end
else
TreeView1.Items.AddChild(ParentNode, SearchRec.name);
end;
IFound := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
var
SearchRec : TSearchRec;
IFound : integer;
newnode : TTreeNode;
begin
IFound := FindFirst(Path + '\*.*', faAnyFile, SearchRec);
While IFound = 0 do
begin
if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
begin
if SearchRec.Attr = faDirectory then
begin
newnode := TreeView1.Items.AddChild(ParentNode, SearchRec.Name);
AddTree(Path + '\' + SearchRec.Name, newnode);
end
else
TreeView1.Items.AddChild(ParentNode, SearchRec.name);
end;
IFound := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;
对几个路径字符串进行操作每次进行判断
我想通过字符串操作,得到这样的一个树型结构显示!那位大虾能搞定???
TForm1 = class(TForm)
TreeView1: TTreeView;
Edit1: TEdit;
Button1: TButton;
ListBox1: TListBox;
procedure Panel1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
procedure TForm1.Button1Click(Sender: TObject);
var
tempList: TStringList;
i: integer;
Node: TTreeNode;
begin
tempList := TStringList.Create;
try
ExtractHttpFields(['\'],[],PChar(Edit1.Text),tempList);
ListBox1.Items.Assign(tempList);
Node := Treeview1.Items.Add(nil,templist.strings[0]);
for i:=1 to tempList.Count -1 do
begin
Node := TreeView1.Items.AddChild(Node,tempList.strings[i]);
end;
finally
tempList.Free;
end;
end;end.
Left = 280
Top = 88
Width = 241
Height = 21
TabOrder = 2
Text = 'c:\test\1\2\1\1\.txt'
end
譬如:c:\test\1\1\1.txt
c:\test\1\2\1\1\.txt
c:\test2\1\2.txt 这样一组数据依次加入TreeView,而且节点不要有重复!
1。假设你的
c:\test\1\1\1.txt
c:\test\1\2\1\1\.txt
c:\test2\1\2.txt
放在ListBox1中
2。假设你有Form1
3。假设你有Button1和TreeView1
procedure TForm1.Button1Click(Sender: TObject);
var
TempList: TStringList;
i,index: integer;
Node,temp: TTreeNode;
begin
for index:=0 to ListBox1.Items.Count-1 do
begin
tempList := TStringList.Create;
try
ExtractHttpFields(['\'],[],PChar(ListBox1.Items[index]),TempList);
Node:=TreeView1.TopItem;
for i:=0 to TempList.Count -1 do
begin
if i=0 then
begin
if Node=nil then
Node:=TreeView1.Items.Add(nil,TempList.Strings[i])
else
if Node.Text<>TempList.Strings[i] then Node:=TreeView1.Items.Add(nil,TempList.Strings[i])
end
else
begin
temp:=Node.GetFirstChild;
while temp<>nil do
begin
if temp.text=TempList.Strings[i] then
begin
Node:=temp;
break;
end;
temp:=temp.GetNextSibling;
end;
if temp=nil then Node:=TreeView1.Items.AddChild(Node,TempList.Strings[i]);
end;
end;
finally
TempList.Free;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
TempList: TStringList;
i,index: integer;
Node,temp: TTreeNode;
begin
for index:=0 to Memo1.Lines.Count-1 do
begin
tempList := TStringList.Create;
try
ExtractHttpFields(['\'],[],PChar(Memo1.Lines[index]),TempList);
Node:=TreeView1.TopItem;
for i:=0 to TempList.Count -1 do
begin
if i=0 then
begin
if Node=nil then
Node:=TreeView1.Items.Add(nil,TempList.Strings[i])
else
begin
temp:=Node;
while temp<>nil do
begin
if temp.Text=TempList.Strings[i] then
begin
Node:=temp;
break;
end;
temp:=temp.getNextSibling;
end;
if temp=nil then Node:=TreeView1.Items.Add(nil,TempList.Strings[i])
end
end
else
begin
temp:=Node.GetFirstChild;
while temp<>nil do
begin
if temp.Text=TempList.Strings[i] then
begin
Node:=temp;
break;
end;
temp:=temp.GetNextSibling;
end;
if temp=nil then Node:=TreeView1.Items.AddChild(Node,TempList.Strings[i]);
end;
end;
finally
TempList.Free;
end;
end;
end;
为了表示我对你的敬意,特意献上我的原创软件http://djvu.cn.gs,欢迎指点!!
[email protected]