Solved

Delphi TTreeView Reading Nodes/SubNodes

Posted on 2009-04-02
3
1,875 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
[X]
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
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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.

749 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