How to add more values to a TreeNode class in C#

You can create a new class which inherits the TreeNode. For each value you want to store in the treenode, create a property for that value. When working with the Treeview, simply cast the TreeNode to your custom TreeNode class.

Example:

public class JobTreeNode : TreeNode {

    private int intField1;

    public int Field1 {
        get {
            return intField1;
        }
        set {
            intField1 = value;
        }
    }
}

Usage (added after comments)

// Add the node
JobTreeNode CustomNode = new JobTreeNode();
CustomNode.Text = "Test";
CustomNode.Field1 = 10
treeView1.Nodes.add(CustomNode);


// SelectedNode 
((CustomNode)(treeView1.SelectedNode)).Field1;

The Tag property of TreeNode is "object". Can't you just store your data in there using a dataclass of some kind?

Tags:

C#

Treeview