Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with Treeview nodes 1

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB
I have a Treeview which I have the code OK to populate as I need. (Copied from tweaked very slightly)

Now I'm trying to extract the info from it.

The parent nodes consist of a letter pair. Children of that letter pair consist of 2 numbers.

like so:
AA
00
01
BA
11
32
47
etc.

I'm trying to read back the data in format as so:
AA00
AA01
BA11
BA32
BA47

I'm only able to get the parent node text out at all, and depending on what I try, usually only the first and second parent as var both in code below.

Could someone tell me where I am going wrong, as to be honest I don't truly get the Treeview and it's nodes..

I'm using while's as I have no idea how many children each parent could have, or how many parents there might be.
Parents and children will always be 2 letters and 2 numbers though.

My code is meant to get first node, if not nil, get the letters and get child numbers. Add the two together and while next child isn't nil, get the next child and add them together, until all nodes are gone through.
The showmessage would be replaced by code to validate and process the letter number combination.

Code:
procedure TMainForm.FindNodesClick(Sender: TObject);
var
  TmpNode, TmpChild: TTreeNode;
  Both, letters: string;
begin
TmpNode := MainForm.TreeView1.Items.GetFirstNode;
while TmpNode <> nil do
  begin
    Letters := TmpNode.Text;
      while (TmpChild <> nil) do
      begin
        TmpChild := TmpNode.GetNextSibling;
        Both := Letters + TmpChild.Text;
        showmessage(Both);
      end;
  end;
end;

Steve (Delphi 2007 & XP)
 
Code:
procedure TMainForm.FindNodesClick(Sender: TObject);
var
  i: integer;
  Both: string;
begin
  for i:= 0 to TreeView1.Items.Count -1 do
    if TreeView1.Items[i].Parent <> nil then begin
      Both:= TreeView1.Items[i].Parent.Text + TreeView1.Items[i].Text;
      showmessage(Both);
    end;
end;
Tested and works

Roo
Delphi Rules!
 

Thanks Roo!

At first I couldn't see how that worked at all, but I think I got it now.
Each child item has a parent property which you are testing here:
Code:
if TreeView1.Items[i].Parent <> nil then begin

The 'both :=' and showmessage are only being executed when items are a child (parent <> nil) and the letters are called from the child's parent property text.

That right?
Seems simple now (assuming I'm right).


Steve (Delphi 2007 & XP)
 
Yes, for your particular data pattern, since you're not adding data, it works.

Items.Count is total of all nodes. Some have parents, some don't.

Nodes are a PITA. I tried GetFirstChild / GetNextChild / GetNextSibling. It got frustrating and Items[] array was just so easy. I only use nodes when adding new.

You'd think a root-node's parent would be the tree but no.

Roo
Delphi Rules!
 

I feel a bit better then, if the nodes were a PITA for you!

Your code is perfect for my needs, and so simple when someone else does it, definately one of those 'wish I'd thought of that' solutions.

TVM.


Steve (Delphi 2007 & XP)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top