delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (5)


A Gardener's Guide To The Tree View: Part IGo to Ghannodahn Kirrel's websiteComponent available for this articleFormat this article printer-friendly!Bookmark function is only available for registered users!
Growing the Trunk and Branches
Product:
Delphi 4.x (or higher)
Category:
VCL-General
Skill Level:
Scoring:
Last Update:
04/10/2001
Search Keys:
delphi delphi3000 article borland vcl code-snippet TreeView Typecast Pointer Kirrel Tutorial VCL
Times Scored:
8
Visits:
9503
Uploader: Ghannodahn Kirrel
Company: RealityCheck Studios
Reference: The Infinity Citadel
Component Download: http://www.icitadel.com/tech_resource/articles/download/treeviewdemo.zip
 
Question/Problem/Abstract:
This is the first in a set of tutorials to be used as reference for manipulation of the TTreeView class in Delphi. In addition to article text, I will include full project source that will make use of the topics discussed in each part of the tutorial.
Answer:



First off, I'd like to go briefly over what a TreeView is and it's potential uses in your application.  For those that aren't familiar with the control, you can very quickly see an example of it by opening your Explorer window (not Internet Explorer, mind you).  The left side of Explorer displays a TreeView starting with the Desktop node as the root.  All TreeView components store hierarchal sets of data, that can have their children collapsed (like your C drive is by default in Explorer), or Expanded (like My Computer is by default).

In your own applications, a TreeView can be used in most cases where you are displaying hierarchal data.  Some simple extensions to the component can yield even greater abilities, such as multiple selection, though we'll save the gory details of this approach for a future tutorial.


A TreeView, beyond the general properties you would expect from any decendant of TWinControl, contains a list of TTreeNode objects.  In order to manage the hierarchy properly, the TTreeNode class defines a property for its parent, among other things.  Conceptually, this approach is rather similar to a discussion board's postings as they would be saved in the database.  If there is sufficient demand for a further description of hierarchal data storage, then I will provide a tutorial focusing on the subject.


This first tutorial will go through the steps of adding root and child nodes.  Because I am not a firm believer in tunnel vision, I have approached this tutorial in such a way that it goes over some concepts that are not limited to the scope of a TreeView.  Typecasting and Pointers are covered lightly, as they are truly necessary for extending the TreeView, as well as many other classes, to their fullest extent.

The two methods of the TTreeNodes (the collection of TTreeNode objects) that you need to concern yourself with are "Add" and "AddChild".  The former is used for adding root-level nodes, or for adding a node at the same level as another.  When I speak of level, I am referring to the depth of the node within the tree view.  For example, consider that we have the following information stored in a tree view:

Shapes
  Cornered
    Triangle
    Square
  Round
    Circle
    Oval

Shapes is a level 1 node, or a "root-level" node.  Cornered and Round are both level 2 nodes, each having the "Shapes" node as a parent.  Triangle and Square are level 3 nodes, having the "Cornered" node as a parent.


I'm going to go briefly over the declaration of the "Add" and "AddChild" methods.  For a more detailed explanation, please refer to the component URL.  I have provided a full Delphi 4 project that makes use of these methods, as well as extensive commenting for each line of processing to better explain to you what's going on and why.


  function Add(Node: TTreeNode; const S: string): TTreeNode;

The Add function is used to add a same-level node, or to add a root node.  The first parameter is a pointer to a node that the new node will be created as a sibling of.  For example, if we had the following TreeNodes already created:

Shapes
  Cornered
    Triangle
    Square

We could add a new item to the same level as the "Triangle" and "Square" nodes by passing a pointer to one or the other to the Add function.

The second parameter is the caption of the new TreeNode object.  Once this function has physically created the new node in the TreeView, it returns a pointer to the new node.


The second method we'll be going over is "AddChild", and has the following declaration:

  function AddChild(Node: TTreeNode; const S: string): TTreeNode;

This is similar to the Add method, except that the first parameter specifies the parent of the new node.  To take our above example, using the AddChild method and passing a pointer to the Shapes node would create a new node at the same level as Cornered.


I hope that this explanation, coupled with the attached project, is of use to Delphi developers.  If the feedback on this tutorial is positive, then I will post additional tutorials going into the murky depths and power the the TreeView control.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
A Gardener's Guide To The Tree View: Part I
    Henry Martin (May 28 2006 6:42AM)

What a load of rubbish! This site is going downhill at a rate of knots. Links to articles don't work. Is this a scam to force people to PAY?


Respond

Link change
    Ghannodahn Kirrel (Sep 13 2000 8:46AM)

I have moved the source downloads to a full-time web server.  This should alleviate any difficulties in downloading the components.  My sincerest apologies for the trouble.

"The line between genius and insanity is measured by success."
Respond

RE: Link change
Omer Can (Apr 10 2001 7:28AM)

Could not download file(s) either.
  What is the right URL?

Omer Can omercan@home.nl
Respond

component download
    Kurt Mueller (Sep 13 2000 4:31AM)

can't download from your IP-Adress
'Page not found'
Kurt
Respond

The file is here
Miguel Angel Garcia Moreno (Nov 22 2004 1:58PM)

http://www.icitadel.com/nwo/technical_detail.asp?uid=E51CC516-BDD4-4102-9061-CCA55F3F25B8
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


   


  Community Ad of
Peganza
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)