Filter a Treeview with a Textbox in a C# winforms app

前端 未结 3 864
渐次进展
渐次进展 2021-01-04 09:41

I have a TreeView in my a C# winform. I would like to be able to add a search functionality through a search box. Basically as the user types in letters (I\'m guessing on th

3条回答
  •  伪装坚强ぢ
    2021-01-04 09:59

    1. Every node in TreeView has Expanded and IsVisible properties. The number of items which are visible at the same time is limited (TreeView.VisibleCount). Based on this information you can reduce the number of nodes to probe dramatically.

    2. When scanning the node and it's child nodes you can abort recursion as you find the first match inside collapsed node, thus you already know it has at least one child and will be visible anyway.

    3. Perform filtering asynchronously. (use new Task() for instance) Start the first task after minimal number of chars was typed (let's say 3). Every next typed char must cancel the running task and start the new one.

提交回复
热议问题