Solved

Delphi TTreeView Reading Nodes/SubNodes

Posted on 2009-04-02
3
1,856 Views
Last Modified: 2013-11-23
I Need some help, I cant find info on this and I could use Assistence.

I have a Treeview that holds User Selectable information.

It Looks Like So

Medicine
      Asprin
      Cough Syrup
      Stool Softener
Drinks                         <---if a user clicks this
      Soda                          <--Then Add this
      Tea                            <--this
      Pop                            <--and this Text to a ListBox

I Need 2 things
If a user Clicks a Level 0 (Medicine, Drinks) Then Read All of the Subnodes from that Node To a ListBox.
If the User Clicks a Level 1 Node (soda, pop, tea, Stool Softener, Ect.), Read the Label of only that Child Node.

It sounds Simple... but I cant seem to get it figured out :( IT was a pain to use a Listview as well.
0
Comment
Question by:jamerslong
  • 2
3 Comments
 
LVL 3

Expert Comment

by:btframework
ID: 24055293
let say your trivew called as TreeView1. Then

var
  Node: TTreeNode;
  Child: TTreeNode;
begin
  Node := TreeView1.Selected;
  if Assigned(Node) then
    if Node.HasChildren then begin
      // If it has child nodes then it is Node[0] in your sample
      // Enum its child nodes
      Child = Node.GetFirstChild;
      while Assigned(Child) do begin
         ListBox1.Items.Add(Child.Text);
         Child := Node.GetNextChild(Child);
      end;
   end else
      // We are here if node has no child node so simple add its text
      ListBox1.Items.Add(Node.Text);
0
 
LVL 3

Accepted Solution

by:
btframework earned 500 total points
ID: 24055322
Forget to say that you have to add this code in OnChange Event.

Like this:

procedure TForm1.TreeView1Change(Sender: TObject; Node: TTreeNode);
var
  Node: TTreeNode;
  Child: TTreeNode;
begin
  Node := TreeView1.Selected;
  if Assigned(Node) then
    if Node.HasChildren then begin
      // If it has child nodes then it is Node[0] in your sample
      // Enum its child nodes
      Child = Node.GetFirstChild;
      while Assigned(Child) do begin
         ListBox1.Items.Add(Child.Text);
         Child := Node.GetNextChild(Child);
      end;
    end else
      // We are here if node has no child node so simple add its text
      ListBox1.Items.Add(Node.Text);
end;
0
 
LVL 4

Author Closing Comment

by:jamerslong
ID: 31566030
Beautiful, Thanks a bunch....
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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

911 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

24 Experts available now in Live!

Get 1:1 Help Now