Solved

Traverse Tree View Nodes

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now