Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Delphi TTreeView Reading Nodes/SubNodes

Posted on 2009-04-02
3
1,870 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

840 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