C# – get the expanding node in a treeview

ctreeview

I have a treeview control that functions like a folder browser.
Because loading the entire folder structer from disk is taking a lot of time i'm trying to load only one level at a time.
So i have a function that adds nodes for all the folders in the current node.

I thought that the best method would be to run it on the BeforeExpand event of the treeview.

UpdateTreeView(TreeView.SelectedNode);

is not working because clicking the + sign to expand is not selecting the node also.

So how to find the node that is expanding.

Best Answer

The BeforeExpand event should work. It has a TreeViewCancelEventArgs which contains a Node property. It is essentially the node being expanded.