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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

treeview basics 2

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
Hi,

I am trying to get familiar with the treeview component.

What I basically try to do is dynamically create 3 root nodes. Add 2 nodes to the first rood node and add nodes to these nodes (3rd level). This third level also needs to get the Data property filled.

for example something like this:

node -- node -- node
| | -- node
| node -- node
| -- node
|
node -- node
|
node

this what I have:
Code:
procedure TForm1.FormShow(Sender: TObject);
  var  MyTreeNode1, MyTreeNode2,MyNode: TTreeNode;
       MyRecPtr: PMyRec;
begin
with TreeView.Items do
  begin
    MyTreeNode1 := Add(nil, 'Messages');
    AddChild(MyTreeNode1,'Type 1');
    AddChild(MyTreeNode1,'Type 2');    
    MyTreeNode1 := Add(nil, 'Signatures');    
    MyTreeNode1 := Add(nil, 'Accounts');
end;
New(MyRecPtr);
  MyRecPtr^.FName := 'checking data 1';
  MyRecPtr^.LName := 'checking data 2';
  MyTreeNode2 := TreeView.Items[0].Item[0];
  TreeView.Items.AddObject(MyTreeNode2, 'Item 1', MyRecPtr);
end;

The first part works well, the second part (using AddObject) not. What I can't figure out is how to access a node (in this example the nodes 'type 1' and 'type 2') and add a child note using AddObject. In the example above the node is added to the node 'messages' instead of to 'type 1'.

Anyone can help me with this?

thanks!
 
As you are trying to add a child node to the second item (the first item being Items[0]) in your TreeView your final two lines of code should be something like:
Code:
  MyTreeNode2 := TreeView.Items[1];
  TreeView.Items.AddChildObject(MyTreeNode2, 'Item 1', MyRecPtr);


Andrew
 
Yes, that worked.

Apparantly the items index works 'top down' and not per level.

What if the first root node has a number of childnodes with more childnotes and you want to add a child node to the second root node? How do you get the item index of the second root node or for example the third root node?
 
I wrote a quick dll with functions like this in ages ago, unfortunatly I don't recall which hard drive it's on (or even which computer) soI can't give you any code that works.

I came up with two ways around this eventually:
1) The easy way, simply scan down the list for the name of the node you want and gets its ID that way.
2) The hard way, pass off all node creation and deletion functions to the dll and let it set up its own indexing scheme that is more use to you.

Either way is a bit of work to do something that really should be built into the component.
 
The following function will provide you with what you want:
Code:
function GetNodeAtLevel ( tv: TTreeView; node, level: integer ): TTreeNode;
var
  n: integer;
begin
  result := nil;
  n := 0;
  while n < tv.Items.count do begin
    if tv.Items[n].Level = level then
      dec ( node );
    if node < 0 then begin
      result := tv.Items[n];
      exit;
    end;
    inc ( n );
  end;
end;
It returns the TreeNode of the nth node at level l. So for example, you wanted to display the text of the 2nd root (i.e. level 0) node then you would call it with:
Code:
var
  tn: TTreeNode;
begin
  tn := GetNodeAtLevel ( TreeView, 1, 0 );
  if tn <> nil then
    ShowMessage ( tn.text )
  else
    ShowMessage ( 'That node does not exist' );
end;
You will need to tidy the code up but it should put you on the right lines.

Andrew
 
Yes, thanks both of you!

I get the idea now. I rewrote the function a bit so you can pass a node name to it. When it finds the match, this node is returned.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top