Solved

Traverse Tree View Nodes

Posted on 2002-05-08
3
1,259 Views
Last Modified: 2007-12-19
I have a treeview and would like to start at the root and traverse each node. I need to do this utilizing the parent child relationship, and will most likely need a recursive function to do so (which backs up). I need to start at the root and visit each of its children and each of theirs and so on. The reason I need this is cause I need to build a visual tree diagram on a canvas, based off from this traverse.

Shane
0
Comment
Question by:shaneholmes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 11

Author Comment

by:shaneholmes
ID: 6997693
My Problem is that when I do something like this to traverse, I have no idea of keeping track of parents and children when drawing the nodes on my canvas. Need someone to finish this by adding the parent and children relationship steps.

CurItem := TreeView1.Items.GetFirstNode;
 while CurItem <> nil do
 begin
  //Draw root node on canvs
  CurItem := CurItem.GetNext;
 end;

Need to know when to draw root, its children, and their children

Shane
0
 
LVL 8

Expert Comment

by:Cesario
ID: 6997791
Shane,
I can send you tomorrow a Working Example ( Printing a Treeview with Quickreport )

I am now too tired  (2:00 am)

cu

Cesario
0
 
LVL 8

Accepted Solution

by:
Cesario earned 500 total points
ID: 6997957
Dear Shane,

Create a new Form
Add TreeView
Add some Items and subitems to the Treeview
Add a Paintbox
Add a Button to start drawing the items on your paintbox

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    Button1: TButton;
    PaintBox1: TPaintBox;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    x,y:Integer;
    Procedure DrawNodes ( Node:TTreeNode; aCanvas:TCanvas);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject );
VAR
  FirstNode : tTreeNode;
begin
  IF TreeView1.Items.count<>0 Then
  Begin
    x := 5;
    y := 5;
    PaintBox1.Canvas.brush.color := clwhite;
    Paintbox1.Canvas.Rectangle( 0,0,Paintbox1.width,Paintbox1.Height);
    FirstNode := TreeView1.Items[0];
    DrawNodes (FirstNode,Paintbox1.canvas );        // Start printing with the root node
  End;
end;

Procedure TForm1.DrawNodes(Node:TTreeNode; aCanvas:TCanvas);
Begin

  While Node <> NIL DO
  Begin

    // ********** Simple Painting *******
    IF Node.Level=0
      then x := 5;
    aCanvas.MoveTo(x+5,y+5);
    aCanvas.LineTo( x+20,y+5);
    inc(x,20);
    aCanvas.TextOut(x,y,node.text);
    Inc(y,18);

    IF Node.HasChildren Then
      Begin
         DrawNodes(Node.getFirstChild, acanvas);
         Node := Node.GetNextChild(Node);
      End
    Else
      Node := Node.GetNextChild(Node);

  End;
End;



end.


you can download the example from
http://www.geocities.com/cesariolab/Project1.zip

good luck

Cesario
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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