Solved

Delphi TTreeView Reading Nodes/SubNodes

Posted on 2009-04-02
3
1,852 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
Comment Utility
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
Comment Utility
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
Comment Utility
Beautiful, Thanks a bunch....
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

763 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

8 Experts available now in Live!

Get 1:1 Help Now