views:

17

answers:

1

I have a program that's watching for changes to certain files in a list of folders, including children (if a folder is watched, it will watch all children). I want to add the ability to exclude certain children from this process, but I'm not sure how best to process it or store the "Rules" for this. For example, here'a a tree I might watch:

Root
  Folder1
    SubFolder1
      File1
      File2
  Folder2
    File3
    File4

I want to allow the user to select a folder (like "Root"), and it automatically selects all the children. If they uncheck a folder (like Folder1), I want to uncheck all the children of that one, but then if they check a child of a child (like File1), I want to check all its children (or, in this case, just watch File1, since it has no children).

I picture that I'll store this data as a list of nodes at which the user has taken some kind of action (either + or -), and walk down the folder list, one level at a time, until I get to a node where the user has taken a different action, then continue to walk into the folders/files further, checking at each step to see if the user has taken some action on that node in particular, and then propagating it to all the sub-nodes. If a new folder/file appears under a watched node, I want to start to watch it, but if it appears under an ignored node, I'll ignore it.

Do I just store each "actioned" folder/file, as well as the action (watch/ignore) that the user took, and then reconstruct the whole tree every time? Is there a more efficient way to do this that I'm not picturing?

I'm happy to clarify if my current explanation of how I picture this unclear. Also, I'm using .NET, but I'm more interested in the general process - I'll work out the implementation.

+1  A: 

You should be able to accomplish this with the TreeView class. From my experience, you'll need to add a bit of logic in there to update parent and child nodes once a node is selected, but this can be accomplished by attaching a handler to one of the TreeView events. In an application I built in the past, I beleive I just used the Click event, but after rethinking that, that "AfterSelect" might have been a better option. However, there might be better events available in the actual TreeNode class, but I have yet to look into these. Hopefully, this post will at least get you started in the right direction.

On a side note, the TreeView class is not marked as serializable. However, you could serialize the root TreeNode's of the TreeView. Therfore, you could save the selected options through either binary serialization, XML serialization, or any other method of your choice.

regex
This is what I ended up doing - the treeview has a set of top-level nodes that correspond to physical disks, and the process serializes each to XML and then saves it to a file. Then I wrote a process that accepts the treenode that's in the treeview (a picture of the disk) and the treenode that you've just de-serialized (the settings from last time) and applies the checkboxes from the saved node to the treeview control. Thanks for pointing me in the right direction!
rwmnau