How to work with TTreeView?

I'm having trouble figuring out TTreeView ...

I really want to know how to add items and subitems and assigning images from an imagelist to them ... all this at runtime.

Can someone please show me some code where this is done, preferably with one button which creates nodes, and then another button which creates subnodes under the selected parent node.

Thanks,

Hagur
LVL 3
hagurAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
hi hagur,
here is example:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    TreeView1: TTreeView;
    ImageList1: TImageList;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  RootNode,
  TreeNode : TTreeNode;
begin
RootNode := Treeview1.Items.Add(nil,'root');
RootNode.SelectedIndex := 1;
RootNode.StateIndex := 0;
with TreeNode do
begin
    TreeNode := Treeview1.Items.AddChild(RootNode,'item1');
    SelectedIndex := 2;
    StateIndex := 3;
    TreeNode := Treeview1.Items.AddChild(RootNode,'item2');
    SelectedIndex := 2;
    StateIndex := 3;
    TreeNode := Treeview1.Items.AddChild(RootNode,'item3');
    SelectedIndex := 2;
    StateIndex := 3;
    end;
 end;

procedure TForm1.Button2Click(Sender: TObject);
var
  newNode : TTreeNode;
begin
 newNode := TreeView1.Selected;
  with newnode do begin
    newNode := Treeview1.Items.AddChild(newnode,'item1');
    SelectedIndex := 4;
    StateIndex := 5;
    newNode := Treeview1.Items.Add(newNode,'item2');
    SelectedIndex := 4;
    StateIndex := 5;
    newNode := Treeview1.Items.Add(newNode,'item3');
    SelectedIndex := 4;
    StateIndex := 5;
    end;
  Treeview1.refresh;
  end;



procedure TForm1.FormCreate(Sender: TObject);
begin
Treeview1.Images  := ImageList1;
end;

end.


i must also mention to visit mike lisches treeview component as it has a lot of added functionality like different fonts per node etc..:
http://www.lischke-online.de/TreeNT.html

Regards Barry




0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hagurAuthor Commented:
Thanks, you've got me started!

-Hagur
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.