Marty
2005-09-17 11:37:29 UTC
Hi,
I have hacked together some code that almost works correctly at loading
a text file or memo strings into a treeview
the test or memo will contain entries in the format below.
Test
.test1
.test2
Test3
.test4
..test5
the resulting treeview should have a structure of
Test
|
--test1
|
--test2
Test3
|
--test4
|
--test5
The code that I have is
procedure TForm1.LoadTree;
procedure AnalyzeEntry (const entry : string; var cap : string; var
level : integer);
var i, lth : integer;
begin
lth := Length (entry);
i := 1;
while (i <= lth) and (entry [i] = '.') do
inc (i);
Level := i - 1;
cap := Copy(entry,i,MaxInt);
end;
procedure Traverse(aNode: TTreeNode; Alist: tstrings; var ix:
integer;level : integer);
var
last : TTreeNode;
lvl : integer;
tmp,cap : string;
begin
last := aNode;
lvl := Integer (Alist.Objects [ix]);
while (lvl >= level) and (ix < Alist.Count) do
begin
if lvl = level
then begin
tmp := Alist[ix];
cap := Copy(tmp, 1, Pos(':', tmp) - 1);
last := TreeView1.Items.AddChild(aNode, cap);
inc(ix);
end
else begin
if lvl > level
then Traverse(last, Alist, ix, level + 1);
end;
if ix < Alist.Count
then lvl := Integer (Alist.Objects [ix]);
end;
end;
var
aCap : string;
i,aLevel : integer;
lst : tStringList;
begin
lst := tStringList.Create;
TreeView1.Items.BeginUpdate;
TreeView1.Items.Clear;
try
for i := 0 to (Memo1.Lines.Count - 1) do
begin
if Memo1.Lines[i]<>'' then
begin
AnalyzeEntry (Memo1.Lines [i], aCap, aLevel);
lst.AddObject (aCap, tObject (aLevel));
end;
end;
i := 0;
while i < Memo1.Lines.Count do
begin
Traverse(TreeView1.Items.Add(NIL, lst[i]), lst,i,0 );
Inc(i);
end;
finally
lst.Free;
end;
TreeView1.Items.EndUpdate;
end;
How would I make this code fully working and also whats the best way of
doing the reverse i.e building the text file from a treeview into the
format above.
Any help would be apprechiated
regards
Martin B
I have hacked together some code that almost works correctly at loading
a text file or memo strings into a treeview
the test or memo will contain entries in the format below.
Test
.test1
.test2
Test3
.test4
..test5
the resulting treeview should have a structure of
Test
|
--test1
|
--test2
Test3
|
--test4
|
--test5
The code that I have is
procedure TForm1.LoadTree;
procedure AnalyzeEntry (const entry : string; var cap : string; var
level : integer);
var i, lth : integer;
begin
lth := Length (entry);
i := 1;
while (i <= lth) and (entry [i] = '.') do
inc (i);
Level := i - 1;
cap := Copy(entry,i,MaxInt);
end;
procedure Traverse(aNode: TTreeNode; Alist: tstrings; var ix:
integer;level : integer);
var
last : TTreeNode;
lvl : integer;
tmp,cap : string;
begin
last := aNode;
lvl := Integer (Alist.Objects [ix]);
while (lvl >= level) and (ix < Alist.Count) do
begin
if lvl = level
then begin
tmp := Alist[ix];
cap := Copy(tmp, 1, Pos(':', tmp) - 1);
last := TreeView1.Items.AddChild(aNode, cap);
inc(ix);
end
else begin
if lvl > level
then Traverse(last, Alist, ix, level + 1);
end;
if ix < Alist.Count
then lvl := Integer (Alist.Objects [ix]);
end;
end;
var
aCap : string;
i,aLevel : integer;
lst : tStringList;
begin
lst := tStringList.Create;
TreeView1.Items.BeginUpdate;
TreeView1.Items.Clear;
try
for i := 0 to (Memo1.Lines.Count - 1) do
begin
if Memo1.Lines[i]<>'' then
begin
AnalyzeEntry (Memo1.Lines [i], aCap, aLevel);
lst.AddObject (aCap, tObject (aLevel));
end;
end;
i := 0;
while i < Memo1.Lines.Count do
begin
Traverse(TreeView1.Items.Add(NIL, lst[i]), lst,i,0 );
Inc(i);
end;
finally
lst.Free;
end;
TreeView1.Items.EndUpdate;
end;
How would I make this code fully working and also whats the best way of
doing the reverse i.e building the text file from a treeview into the
format above.
Any help would be apprechiated
regards
Martin B