dannyhut
dannyhut

Reputation: 301

treenode's treeview property. How is this set?

I'm designing custom TreeNodes at the moment and have come up with a bit of a mystery.

TreeNode's TreeView property is a public property that can be used to get the TreeView the TreeNode belongs to. What I can't seem to find is how is this set.

I assume that it is set by the TreeNodeCollection when you call one of it's Add methods though I can't find how the TreeView control passes a reference to itself to the TreeNode through the collection. Is there an undocumented method being used or something else. Or mabey I missing the bleeding obvious again, I am known for doing that.

Thanks for any help Danny

Upvotes: 0

Views: 208

Answers (2)

Robert Harvey
Robert Harvey

Reputation: 180908

TreeNode contains an internal constructor that looks like this:

internal TreeNode(TreeView treeView) : this()
{
    this.treeView = treeView;
}

It is called by the TreeView object to create a root node:

root = new TreeNode(this);

And of course, if the treeView member is not set, it will be set the first time you try to retrieve it from the property:

public TreeView TreeView {
    get {
        if (treeView == null)
            treeView = FindTreeView();
        return treeView;
    }
}

Upvotes: 1

shree.pat18
shree.pat18

Reputation: 21757

As per the source code, an internal method FindTreeView is used to recursively get the parent of a given TreeNode. You can take a look at the source here

public TreeView TreeView {
        get {
            if (treeView == null)
                treeView = FindTreeView();
            return treeView;
        }
    }

Note: Snippet above is from the source link posted above.

Additionally, as Robert Harvey has explained in his answer, it can also be set through an internal constructor:

internal TreeNode(TreeView treeView) : this() {
        this.treeView = treeView;
    }

Upvotes: 0

Related Questions