Solved

Tree Views

Posted on 1998-05-10
5
222 Views
Last Modified: 2010-04-06
Hi All.

I need some help :)  How the hell do you use Tree Views?  I cant any helpfull documentation on them anywhere.

Can you tell me how to:

Add items, insert and delete.  Also, how do I add a child at a certain point.  Ie, I want to add Fred to Group1 which is the third item item in a list to 20.

Thanks.


Stuart.
0
Comment
Question by:Stuart_Johnson
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 50 total points
ID: 1344148
Hi Stuart

Here is the answer to a previous question I answered:

Delphi's TTreeView (which is a BIG improvement over Delphi 1's TOutline component) has a property "Items" of type TTreeNodes.  Most of the work is done with the items property.

Consider the following:
(Form1 has a TTreeView called tv.)

procedure TForm1.AddSomeNodes;
var
    RootNode, Child1Node, Child2Node, GrandChildNode: TTreeNode;
begin
    RootNode := tv.Items.AddChild(nil, 'This is the root');
    Child1Node := tv.Items.AddChild(RootNode, 'This is a child');
    Child2Node := tv.Items.AddChild(RootNode, 'Second child');
    GrandChildNode := tv.Items.AddChild(Child1Node, 'Child of Child 1');
end;

From this you can see that to add a tree node you write:
    MyNewNode := tv.Items.AddChild(ParentNode, 'Caption');
If ParentNode = nil then the item is added to the root of the tree.

Each node in the treeview is of type TTreeNode.  TreeNode has some useful methods, e.g. Expand, Collapse, GetFirstChild, HasAsParent, etc.

If you run the procedure AddSomeNodes above, you will see that RootNode is collapsed.  To expand it with code, call "RootNode.Expand(False);".  To expand the node AND all its children nodes, call "ANode.Expand(True);".  To collapse the node again, call "ANode.Collapse;".

To go through the immediate children of any node, do the following: (This code goes through all the children of the selected node):
procedure TForm1.ShowSelectedChildren;
var
  ParentNode, ChildNode: TTreeNode;
begin
  ParentNode := tv.Selected;
  if (ParentNode <> nil) then begin
    ChildNode := ParentNode.GetFirstChild;
    while (ChildNode <> nil) do begin
      ShowMessage(ChildNode.Text);
      ChildNode := ChildNode.GetNextSibling;
    end;
  end;
end;


Assigning images to a TTreeView is similar to Delphi's TListView.  Put a TImageList on the form and add a few bitmaps/icons to it.  Set the treeview's Images property to the imagelist.  When you add a TreeNode you can set its ImageIndex and SelectedIndex properties.  The ImageIndex property determines which image in the TImageList the node will use when it is not selected.  The SelectedIndex property determines which image the node will use when it is selected.

E.g.
MyNewNode := tv.Items.AddChild(ParentNode, 'New Node');
MyNewNode.ImageIndex := 5;
MyNewNode.SelectedIndex := 5;

To change the display text of a node:
MyNode.Text := 'Some new caption';

Other useful properties of the TTreeView are ReadOnly (user cannot edit a node's text),  ShowButtons and ShowLines (determine whether lines connecting nodes are shown.  If you open any Windows 95 help file with a contents you will see that the contents is a treeview with ShowButtons = False and ShowLines = False).

This should get you started.  Let me know if I can be of more help.

JB
 

0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 1344149
Once again, you have excelled!!  Full points.

I would be interested to know how I can implement drag and drop, but I will dig further through my documentation.  If I can find out then, I'll repost.

thanks again.

Stu
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1344150
Hi Stu

Before you repost a drag-drop sample, I can e-mail you one I did for someone else.  I don't remember who it was, but I keep all the samples.  Is your e-mail address still stuartj@futureschool.com.au?

Dave
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 1344151
Certainly is, and damn it, I just posted a question on that one too.

If you can answer the other questions, do so and I'll give you the points anyway :)

Can you send me that file today?  I just want to get this thing finished!

Thanks for your help.  Much appreciated.

Stuart.
0
 
LVL 5

Expert Comment

by:JimBob091197
ID: 1344152
I have sent you the sample, and I will look at the other question.

Cheers,
JB
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 83
LAN or WAN ? 11 82
Base1 Encode/Decode 3 67
IdTCPClient and IdTCPServer exchanging data on desktops and not Android 11 91
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

910 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

21 Experts available now in Live!

Get 1:1 Help Now