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

TreeView - Delphi 7 - Fairly Newbie 2

Status
Not open for further replies.

toetag

MIS
Sep 27, 2002
166
US
How can I get a Node by clicking? How can i get a Nodes' child by clicking?

In the help files, i find an event called: OnItemClick but I can't use it. (ie: i type: tvwEditor. and the "dot completion" (as I call it) doesn't show OnItemClick. Just OnClick or OnDblClick).
 
toetag,

Use the treeView's Selected property to get access to the currently selected node. Example:

Code:
   str := TreeView1.Selected.Text;

In addition, use Selected.Item list to walk through any items on the selected node. For example, this code sample expands any child items below the selected node in a TreeView:

Code:
var
   li : LongInt;
begin
                  
   with TreeView1.Selected do
   begin
     if not Expanded then Expanded := TRUE;
   
     for li := 0 to Count - 1 do
     begin
        if Item[ li ].HasChildren and 
           not Item[ li ].expanded then
              Item[ li ].expanded := TRUE;
     end;
   end;
end;

Hope this helps...

-- Lance
 
Thank you Lance. Took me a few tries to get it right but it finally did work. (I was looking for the index number of the node to do a search on a custom objects TObjectList).

Both examples are what I needed to get me past this obstacle. I'm sure there will be more.
 
Hi,
It's all more easy:

procedure TForm1.TreeView1Click(Sender: TObject);
Var n,nChld:TTreeNode;
begin
If TreeView1.Selected = Nil
Then Exit;

N:=TreeView1.Selected;
If n.HasChildren
Then nChld:=N.getFirstChild;

//Eventually for all the children
//nChld:=nChld.getNextSibling; //Now nChld is the brother

end;


Ciao,
GeppoDarkson

 
Thank you GD. That is easy. I was trying to figure out how the OnClick was passing the node. The OnClick, isn't. In the OnClick, it's looking at the treeviews selected. And here I spent hours and hours (okay so not that many), trying to figure out how to do it. Then with Lance's help, i found a workd around.

I'm going to use your example and see how she goes. (i'm sure she'll work just fine).

Toetag
 
Hi,

TreeView1.GetNodeAt(x,y)

gives you the node (if any, else Nil) under the coordinates x,y.

If you select more then one node (with TreeView1.MoltiSelect=True) you can use
SelNode:=TreeView1.GetSelections(MyList);
Fills MyList with the nodes and returns in SelNode the node at the TreeView1.Selected property.

Genrally speaking, when working with nodes, you use a method or property of the TTreeView to locate a particular node, then you navigate trough the tree with TTreeNode methods:
Node2:=Node1.GetNext // GetNextChild //Parent etc.

Ciao,
Geppo Darkson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top