Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Traverse Tree View Nodes

Posted on 2002-05-08
3
Medium Priority
?
1,274 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

715 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