I'm using a Treeview for the first time ever, so it's a bit alien to me.
I've got the code below from this site ( ) and modified it to do what I want, almost.
Here is my listbox data:
[blue]
BA00zz
AB23yb
QF01nn
AA00zz
ZZ76aa
AA04zz
AA02zz[/blue]
When the code is run, I get a node AA with 3 children, 00, 04 and 02, another node AB with the child 23, and so on, but in the order they appear in the list (fair enough).
My problem is, whilst I've worked out how to tweak the code I found to add the nodes in the format I want, I don't know enough to tweak it to add them sorted.
I want node AA to be inserted at the top, with 00, 02 and 04 in ascending order, and so on.
Could someone point me in right direction pls?
Steve (Delphi 2007 & XP)
I've got the code below from this site ( ) and modified it to do what I want, almost.
Here is my listbox data:
[blue]
BA00zz
AB23yb
QF01nn
AA00zz
ZZ76aa
AA04zz
AA02zz[/blue]
When the code is run, I get a node AA with 3 children, 00, 04 and 02, another node AB with the child 23, and so on, but in the order they appear in the list (fair enough).
My problem is, whilst I've worked out how to tweak the code I found to add the nodes in the format I want, I don't know enough to tweak it to add them sorted.
I want node AA to be inserted at the top, with 00, 02 and 04 in ascending order, and so on.
Could someone point me in right direction pls?
Code:
procedure TSDIAppForm.Button1Click(Sender: TObject);
var
i: integer;
str, first, second: string;
CurrNode, PrntNode: TTreeNode;
begin
TreeView1.Items.Clear;
try
for i := 0 to ListBox1.Items.Count-1 do
begin
first := AnsiLeftStr(ListBox1.Items[i], 2);
second := AnsiMidStr(ListBox1.Items[i], 3, 2);
PrntNode := nil;
CurrNode := GetNode(PrntNode, first);
//if node doesn't already exist then create it
if CurrNode = nil then
CurrNode := TreeView1.Items.AddChild(PrntNode, first);
PrntNode := CurrNode;
CurrNode := GetNode(PrntNode, second);
//if node doesn't already exist then create it
if CurrNode = nil then
CurrNode := TreeView1.Items.AddChild(PrntNode, second);
PrntNode := CurrNode;
end;
finally
end;
end;
function GetNode(ParentNode: TTreeNode; NodeName: string): TTreeNode;
var
TmpNode: TTreeNode;
begin
if ParentNode = nil then
TmpNode := SDIAppForm.TreeView1.Items.GetFirstNode
else
TmpNode := ParentNode.GetFirstChild;
while (TmpNode <> nil) and (TmpNode.Text <> NodeName) do
TmpNode := TmpNode.GetNextSibling;
Result := TmpNode;
end;
Steve (Delphi 2007 & XP)