Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help inserting/sorting with Treeviews

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB
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?
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)
 
Check out the SortType property of the parent TTreeView.

Hope this helps.
 
Thanks for that.
I had tried that already by setting the sorttype at design time, but I hadn't counted on the way it works. I set the sort type to desired, but it did nothing so I assumed I was barking up the wrong tree.

To get it to work, I've added these lines:
Code:
  TreeView1.SortType := stNone;
  TreeView1.SortType := stText;

I guess that's the way to do it?

If my treeview gets large, will I need a method to speed up things, such as begin/end update ?


Steve (Delphi 2007 & XP)
 
I've always managed to avoid using the beast... So I don't know why it has that behavior.

If my treeview gets large, will I need a method to speed up things, such as begin/end update ?

You should do that anyway (right from the start, instead of trying to fit it into existing code). All the big memory-hog string components like TListView, TStringGrid, etc. get really slow on large data without BeginUpdate and EndUpdate (because it otherwise endeavors to update the visual part each time a change is made).

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top