2

I show items in a TTreeView object. When an Item has children the control paints a > next to the icon (or a down pointing arrow if expanded).

I was wondering if I can tell the Item somehow to paint the > even if no children have been added (yet).

There are certain conditions in my software where it makes sense to show the user there are children, without actually adding the children yet (That is then done when the item is selected)

Using c++ Builder 2009 VCL but this Q should be valid for Delphi as well.

Peter
  • 1,334
  • 12
  • 28
  • 1
    Perhaps, adding an empty item (no caption, no image), which you then discard when the actual child is fetched and shown – Tom Brunberg Nov 08 '18 at 23:09
  • 1
    Perfectly possible in OnExpanding – Sertac Akyuz Nov 08 '18 at 23:17
  • Possible duplicate of [How to improve performance of populating a massive tree view?](https://stackoverflow.com/questions/19943503/how-to-improve-performance-of-populating-a-massive-tree-view) – Jerry Dodge Nov 09 '18 at 16:10
  • @JerryDodge I don't see this as a duplicate no. There is certainly overlap but the Q comes from a different angle and the accepted answer is different too. – Peter Nov 12 '18 at 04:03

1 Answers1

11

In VCL, TTreeNode has a HasChildren property:

Indicates whether a node has any children.

HasChildren is true if the node has subnodes, or false if the node has no subnodes. If ShowButtons of the tree view is true, and HasChildren is true, a plus (+) button will appear to the left of the node when it is collapsed, and a minus (-) button will appear when the node is expanded.

Note: If a node has no children, setting HasChildren to true will show a (+) plus button, but will not add any child nodes and the node cannot be expanded.

So, you can set a node's HasChildren to true before actual child nodes have been created for it. Then later on, once you have determined whether the node has any actual child nodes, you can reset HasChildren to false if there are no child nodes present.

Despite what the documentation suggests above, attempting to expand a node that has no child nodes but does have HasChildren set to true WILL trigger the TTreeView.OnExpanding event, at least. That is a good place to populate the actual child nodes and update HasChildren.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770