How best to update "in place" a TreeCtrl?

I have constructed a tree view of a directory of files. It looks great. But now how do I tell it to update when the files on disk may have changed? I think that I could just rebuild the whole tree again, but that seems wasteful and will probably lose my user’s expanded/collapsed selections, right? How do I update the tree “in place” so that the user’s expanded/collapsed nodes stay that way but with the new items now present?

Code snippets would be fantastic, but just a discussion of the general procedure would help me understand if I am on the right track. Thanks.

David Lynch wrote:

I have constructed a tree view of a directory of files. It looks great.
But now how do I tell it to update when the files on disk may have
changed? I think that I could just rebuild the whole tree again, but
that seems wasteful and will probably lose my user's expanded/collapsed
selections, right? How do I update the tree "in place" so that the
user's expanded/collapsed nodes stay that way but with the new items now
present?

Code snippets would be fantastic, but just a discussion of the general
procedure would help me understand if I am on the right track. Thanks.

Two things to consider:

1. If you're not already doing it you should not be adding nodes to the tree until the parent item where they will appear (for example the parent folder for a bunch of files) is expanded. This will enable you to not have to read the whole tree of files at the start and instead just read one folder at a time on-demand. You can also remove the child nodes for a folder when that fodler's tree item is collapsed. That will help with keeping memory use down. To be able to indicate that an item does have children without actually adding them to the tree you can use SetItemHasChildren.

2. For those tree items that are expanded you will probably want to periodically scan the disk folder and see if there are new files or subfolders present or existing ones removed. For those you can add new nodes to the tree with InsertItem or remove nodes with the Delete method.

···

--
Robin Dunn
Software Craftsman