How to: Add Custom Information to a TreeView or ListView Control (Windows Forms)
You can create a derived node in a Windows Forms TreeView control or a derived item in a ListView control. Derivation allows you to add any fields you require, as well as custom methods and constructors for handling them. One use of this feature is to attach a Customer object to each tree node or list item. The examples here are for a TreeView control, but the same approach can be used for a ListView control.
To derive a tree node
To use a derived tree node
You can use the new derived tree node as a parameter to function calls.
In the example below, the path set for the location of the text file is the My Documents folder. This is done because you can assume that most computers running the Windows operating system will include this directory. This also allows users with minimal system access levels to safely run the application.
// You should replace the bold text file // in the sample below with a text file of your own choosing. // Note the escape character used (@) when specifying the path. treeView1.Nodes.Add(new myTreeNode (System.Environment.GetFolderPath _ (System.Environment.SpecialFolder.Personal) _ + @"\TextFile.txt") );
If you are passed the tree node and it is typed as a TreeNode class, then you will need to cast to your derived class. Casting is an explicit conversion from one type of object to another. For more information on casting, see Implicit and Explicit Conversions (Visual Basic) (Visual Basic), () Operator (C# Reference) (Visual C#), or Cast Operator: () (Visual C++).