I have a treeview that utilizes the StateImageList to implement “proper” tri-state checks in a tree. The problem however is the first layer of nodes that get added to the tree. By default the StateImageIndex on a new TreeNode is -1, and I have no way of really knowing that they have been added. Nodes that are child nodes I can catch on the BeforeExpand and set the StateImageIndex properly but I cant seem to figure out what to do about the Top Layer of nodes.
My current work around (that I really really hate) is I have a timer running in the background checking to see if the number of nodes in the treeview has changed, if it has I run through and find all the root nodes that have a StateImageIndex set to -1 and set them to 0. It works, but it seems like a terrible waste of resources. Any one have a better suggestion?
No event, you can’t generate one either. The native control doesn’t support an event because only code can add nodes, not the user. You can’t generate one because you can’t break into the TreeNodeCollection class. A simple workaround is to add a static CreateNode() factory method to your class with the expectation that the client code uses that to create a properly initialized node.
You ought to consider the DrawMode property so you can just draw the node the way you want it. HitTest() to detect clicks, ControlPaint.DrawCheckBox() to draw the box.