views:

554

answers:

1

Hello everybody,

Whats the best/efficient way to filter a Treeview nodes using WinForms.TreeView?

For example: I typed "abc" and only the Nodes contained "abc" become visible. Then I typed "abcd", and I should to see the only Nodes containing "abcd" text. And so on, so each time I changed filter criterion the treeview content is also changes.

Any ideas?

-- Best wishes, Murat

+3  A: 

If you are looking for the best performance, clone the tree, then remove all the items from the cloned tree, then simply replace the existing tree with the cloned (and filtered) one.

I also keep a backing tree that is always unfiltered.

This works well for me for quite big trees with 1000 - 2000 nodes.

leppie
I agree with this approach, there is a pattern related to it called the memento pattern.
Burt
It's not pretty, it would have been so much nicer to have a 'Visible' property on a TreeNode.
leppie
I'll tested this method. Thanks for the advice. I am planning to implement this feature on the TreeView with up to 10-20k nodes...
Murat
+1 What behaviour did you implement for the situation where a parent *does not* match the criteria, but a child of that parent *does* match the criteria?
Jon Seigel
@John: I do not have that scenario :) Filter only leaves.
leppie
@Murat: Maybe TreeViewAdv would be a better choice.
leppie