Solved

Traverse Tree View Nodes

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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