Coming from Win32, I am having bit of an issue or a problem trying to work with treeview for .NET. I have searched online, msdn library and stackoverflow and none seem to help me.
Could someone, please, show me how you can use Treeview to create node, create childnode, set data or object, retrieve object, check its level, etc...
Example code would be greatly appreciated. Thank you.
1) Create a root Node
use the Nodes.Add
method
treeView1.Nodes.Add('A Node');
2) Create a Child Node, get the instance to the parent node and uses the Nodes.Add
Node.Nodes.Add('A Child Node');
3) To store an object in anode use the Tag
property of the Node
Node.Tag:=MyObj;
4) To Retrieve the object use the tag property and cast the value.
MyObj2:= TMyObject(Node.Tag);
5) to get the level, chek the Level
property of the node.
Check this simple code :
Var
Node : TreeNode;
MyObj : TMyObject;
MyObj2 : TMyObject;
begin
Node:=treeView1.Nodes.Add('A Node'); //add a root node
Node.Text:= String.Format('{0} in level {1}',Node.Text,Node.Level);
Node.Nodes.Add('A Child Node');
Node.Nodes.Add('Another Child Node');
Node:=treeView1.Nodes.Add('Another Node');
MyObj:=New TMyObject;
MyObj.Foo:='Hello';
Node.Tag:=MyObj;//set the object
MyObj2:= TMyObject(Node.Tag); //get the assigned object
MessageBox.Show(MyObj2.Foo);
end;