Solved

Traverse Tree View Nodes

Posted on 2002-05-08
3
1,249 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
  • 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10 Round Image 2 129
Run video youtube webbrowse 10 62
How to build JSON File in Delphi 6 3 35
Wincontrol not (correctly) drawn 15 38
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

832 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