[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

TTreeView? How it works?

Posted on 1997-12-09
5
Medium Priority
?
422 Views
Last Modified: 2010-04-04
I have no clue on how the TTreeView works... I've looked into Delphi2, Secrets of Delphi2, Delphi Programming for Dummies and I've also looked into the Delpi on line help with little to know results.  HELP!!!!!  Need info. on all area of TTreeView.
0
Comment
Question by:PPaul
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 800 total points
ID: 1353257
Delphi's TTreeView (which is a BIG improvement over Delphi 1's TOutline component) has a property "Items" of type TTreeNodes.  Most of the work is done with the items property.

Consider the following:
(Form1 has a TTreeView called tv.)

procedure TForm1.AddSomeNodes;
var
    RootNode, Child1Node, Child2Node, GrandChildNode: TTreeNode;
begin
    RootNode := tv.Items.AddChild(nil, 'This is the root');
    Child1Node := tv.Items.AddChild(RootNode, 'This is a child');
    Child2Node := tv.Items.AddChild(RootNode, 'Second child');
    GrandChildNode := tv.Items.AddChild(Child1Node, 'Child of Child 1');
end;

From this you can see that to add a tree node you write:
    MyNewNode := tv.Items.AddChild(ParentNode, 'Caption');
If ParentNode = nil then the item is added to the root of the tree.

Each node in the treeview is of type TTreeNode.  TreeNode has some useful methods, e.g. Expand, Collapse, GetFirstChild, HasAsParent, etc.

If you run the procedure AddSomeNodes above, you will see that RootNode is collapsed.  To expand it with code, call "RootNode.Expand(False);".  To expand the node AND all its children nodes, call "ANode.Expand(True);".  To collapse the node again, call "ANode.Collapse;".

To go through the immediate children of any node, do the following: (This code goes through all the children of the selected node):
procedure TForm1.ShowSelectedChildren;
var
  ParentNode, ChildNode: TTreeNode;
begin
  ParentNode := tv.Selected;
  if (ParentNode <> nil) then begin
    ChildNode := ParentNode.GetFirstChild;
    while (ChildNode <> nil) do begin
      ShowMessage(ChildNode.Text);
      ChildNode := ChildNode.GetNextSibling;
    end;
  end;
end;


Assigning images to a TTreeView is similar to Delphi's TListView.  Put a TImageList on the form and add a few bitmaps/icons to it.  Set the treeview's Images property to the imagelist.  When you add a TreeNode you can set its ImageIndex and SelectedIndex properties.  The ImageIndex property determines which image in the TImageList the node will use when it is not selected.  The SelectedIndex property determines which image the node will use when it is selected.

E.g.
MyNewNode := tv.Items.AddChild(ParentNode, 'New Node');
MyNewNode.ImageIndex := 5;
MyNewNode.SelectedIndex := 5;

To change the display text of a node:
MyNode.Text := 'Some new caption';

Other useful properties of the TTreeView are ReadOnly (user cannot edit a node's text),  ShowButtons and ShowLines (determine whether lines connecting nodes are shown.  If you open any Windows 95 help file with a contents you will see that the contents is a treeview with ShowButtons = False and ShowLines = False).

This should get you started.  Let me know if I can be of more help.

JB
0
 

Author Comment

by:PPaul
ID: 1353258
You make it easy to understand....  The above code for serching throught a nods child how do I do it without the selected part?

P.S.  I have also unlocked the How to hide a form on start-up...
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1353259
All the nodes are of type TTreeNode.  I use tv.Selected to get one TTreeNode.  To search through a node's children without using the selected part, you could use variables to store each node you add (e.g. ANode := tv.Items.AddChild(SomeParentNode, 'A Node');) or you could go through the items until you found the node you wanted (e.g. for i := 0 to tv.Items.Count - 1 do if tv.Items[i].Text = 'Some Node Text' then ...)

Either way, you would have to get a TTreeNode to work with.  Once you have a TTreeNode you can use that node to go through its children nodes.

JB
0
 

Author Comment

by:PPaul
ID: 1353260
Thanks JimBob.... If I have anymore questions about TTreeView can I get you E-Mail Address....

Here's Mine.   920595@ican.net...

thanks...
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1353261
Sorry about the delay - I've been away for a couple of weeks.

My e-mail address is davekw@iafrica.com.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month18 days, 10 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question