views:

232

answers:

3

So - working with C# and Windows Forms, I have a bunch of "Task" classes. Every class has a List<Task>, so that they can have any number of children (and the children can obviously have more children...)

These tasks are stored in a "Project" class (in a List<Task>, obviously), which is then serialized to XML. I have a function to recurse through the tasks and put them all in a TreeView. It works just like it should.

But the thing is - if I select a task from the TreeView, how will I find it from the Project? I thought of some kind of foreign key (which would be the TreeNode's key?), but since I'm using XML for serialization, that's not possible.

So, what should I do?

+1  A: 

You have to give each Task a unique key (store them in a Dictionary), then set that int value to the TreeNode's tag.

MiffTheFox
+1  A: 

Every tree node have FullPath property (which represent path from root to node), you can create dictionary, and fill it with your tasks, using FullPath as the key. Then, when node clicked, you take node FullPath and extract task from dictionary.

arbiter
+3  A: 

I would put a reference to the "Task" object in the Tag member of each TreeNode. It then becomes very easy to cast the Tag to a Task and use the Task when handling any Tree event.

NascarEd
Upvoted. Simplest solution always the best :)
arbiter